如何显示基于链接的 ID

How to display links based id

我有一个显示标题和一组链接的页面。它看起来像这样:

现在他们显示的是与整个页面相关的所有链接,而不是与个别标题相关的链接。我希望每个标题只有与其相关的链接。我正在使用 rails 4,cocoon 和 haml。关于我该怎么做的任何想法?

essential.rb

class Essential < ActiveRecord::Base

    has_many :favorites, :dependent => :destroy
    has_many :catalogs, :dependent => :destroy
    has_many :labels, :dependent => :destroy
    has_many :members,:dependent => :destroy
    has_many :sub_catalogs, through: :catalogs, :dependent => :destroy
    has_many :sub_favorites, through: :favorites, :dependent => :destroy
    has_many :instruments, through: :members, :dependent => :destroy 

    belongs_to :user 

    accepts_nested_attributes_for :favorites, :reject_if => :all_blank, :allow_destroy => true
    accepts_nested_attributes_for :catalogs, :reject_if => :all_blank, :allow_destroy => true
    accepts_nested_attributes_for :labels, :reject_if => :all_blank, :allow_destroy => true
    accepts_nested_attributes_for :members, :reject_if => :all_blank, :allow_destroy => true
    accepts_nested_attributes_for :sub_catalogs, :reject_if => :all_blank, :allow_destroy => true
    accepts_nested_attributes_for :sub_favorites, :reject_if => :all_blank, :allow_destroy => true
    accepts_nested_attributes_for :instruments, :reject_if => :all_blank, :allow_destroy => true

    validates :band_name, :bio, :image, presence: true 

    has_attached_file :image, styles: { medium: "400x400#" }
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

catalog.rd

class Catalog < ActiveRecord::Base
    belongs_to :essential
    has_many :sub_catalogs, :dependent => :destroy

    accepts_nested_attributes_for :sub_catalogs, :reject_if => :all_blank, :allow_destroy => true
end

sub_catalog.rb

class SubCatalog < ActiveRecord::Base
    belongs_to :catalog
end

essentials_controller.rb

class EssentialsController < ApplicationController

    before_action :find_essential, only: [:show, :edit, :update, :destroy]


    def index
        @essential = Essential.all.order("created_at DESC")
    end

    def new
        @essential = current_user.essentials.build
    end

    def show

    end

    def create
        @essential = current_user.essentials.build(essential_params)

        if @essential.save
            redirect_to @essential, notice: "Successfully created new essential"
        else
            render 'new'
        end
    end

    def edit
    end

    def update
        if @essential.update(essential_params)
            redirect_to @essential
        else
            render 'edit'
        end
    end

    def destroy
        @essential.destroy
        redirect_to root_path, notice: "Successfully deleted Essential"
    end


    private

    def essential_params
        params.require(:essential).permit(:band_name, :bio, :image, :country, :album, 
            favorites_attributes: [:id, :song_title, :url, :url_type, :_destroy, sub_favorites_attributes: [:id, :fav_url, :fav_url_type, :_destroy]],
            members_attributes: [:id, :band_member, :position, :_destroy, instruments_attributes: [:id, :position, :_destroy]], 
            labels_attributes: [:id, :record_label, :_destroy], 
            catalogs_attributes: [:id, :song_name, :_destroy, sub_catalogs_attributes: [:id, :sub_url, :sub_url_type, :_destroy] ])
    end

    def find_essential
        @essential = Essential.find(params[:id])
    end
end

show.html.haml

.main_content
    #essential_top.row
        .col-md-4
            = image_tag @essential.image.url(:medium), class: "essential_image"
        .col-md-8
            #essential_info
                %h1= @essential.band_name
                %p.bio= @essential.bio


    .row
        .col-md-6
            #favorites
                %h2 Favorites
                %table
                    - @essential.favorites.each do |favorite|
                        %tr
                            %td= favorite.song_title
                            - @essential.sub_favorites.each do |sub_favorite|
                                %td= link_to sub_favorite.fav_url_type, "https://www.#{sub_favorite.fav_url}"

        .col-md-6
            #catalogs
                %h2 Catalogs
                %table
                    - @essential.catalogs.each do |catalog|
                        %tr
                            %td= "#{catalog.song_name} |"
                            - @essential.sub_catalogs.each do |sub_catalog|
                                %td= link_to sub_catalog.sub_url_type, "https://www.#{sub_catalog.sub_url}"
    .row
        .col-md-6
            #labels
                %h2 Record Label(s)
                %table
                    - @essential.labels.each do |label|
                        %tr
                            %td= label.record_label

        .col-md-6
            #members
                %h2 Members
                %table
                    - @essential.members.each do |member|
                        %tr
                            %td= member.band_member
                            - @essential.instruments.each do |instrument|
                                %td= instrument.position



        .col-md-12
            = link_to "Back", root_path, class: "btn btn-secondary"
            - if user_signed_in?
                = link_to "Edit", edit_essential_path, class: "btn btn-secondary"
                = link_to "Delete", essential_path, method: :delete, data: {confirm: "Are you sure?" }, class: "btn btn-secondary"

form.html.haml

= simple_form_for @essential,  html: { multipart: true } do |f|
    - if @essential.errors.any?
        #errors
            %p
                = @essential.errors.count
                Prevented this essential from saving
            %ul
                - @essential.errors.full_message.each do |msg|
                    %li = msg
    .panel-body
        = f.input :band_name,  placeholder: "Band Name", label: false, input_html: { class: 'form-control form-inline'}
        = f.input :bio, placeholder: "Bio", label: false,  input_html: { class: 'form-control'}
        = f.input :image,  placeholder: "Image", label: false, input_html: { class: 'form-control'}
        = f.input :country, collection: ["England", "United States", "Ireland", "Germany", "France", "Finalnd", "Sweden", "Wales", "Scotland", "Denmark", "Iceland", "Spain", "Italy"], input_html: { class: "form-control form-input" }
        = f.input :album, collection: 1..25, input_html: { class: "form-control" }


        .row
            .col-md-6
                %h3 Favorites
                #favorites
                    = f.simple_fields_for :favorites do |favorite|
                        = render 'favorite_fields', f: favorite
                    .links
                    = link_to_add_association 'Add Favorite', f, :favorites, class: 'btn btn-secondary add-button'

            .col-md-6
                %h3 Catalog
                #catalogs
                    = f.simple_fields_for :catalogs do |catalog|
                        = render 'catalog_fields', f: catalog
                    .links
                    = link_to_add_association 'Add Catalog', f, :catalogs, class: 'btn btn-secondary add-button'
        .row
            .col-md-6
                %h3 Record Label(s)
                #labels
                    = f.simple_fields_for :labels do |label|
                        = render 'label_fields', f: label
                    .links
                    = link_to_add_association 'Add Record Label', f, :labels, class: 'btn btn-secondary add-button'
            .col-md-6
                %h3 Band Members
                #members
                    = f.simple_fields_for :members do |member|
                        = render 'member_fields', f: member
                    .links
                    = link_to_add_association 'Add Band Member', f, :members, class: 'btn btn-secondary add-button'






    = f.button :submit, class: 'btn btn-primary'

在您的视图文件中,查看目录部分并将其更改为

%h2 Catalogs
%table
  - @essential.catalogs.each do |catalog|
    %tr
      %td= "#{catalog.song_name} |"
      - catalog.sub_catalogs.each do |sub_catalog|
        %td= link_to sub_catalog.sub_url_type, "https://www.#{sub_catalog.sub_url}"

@essential更改为局部变量catalog