Доступ к рельсам данных вложенной формы 4

У меня есть форма с вложенными полями с другим вложенным полем внутри этого. Все данные формы сохраняются в БД, но моя проблема в том, что я не могу отображать данные второго вложенного поля в своем представлении. У меня есть базовая модель, в которой много каталогов, а в каталогах много подкаталогов. Я использую рельсы 4, кокон и HAML.

Основной контроллер

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], members_attributes: [:id, :band_member, :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

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
    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
    validates :band_name, :bio, :image, presence: true 

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

Каталог.rb

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.rd

class SubCatalog < ActiveRecord::Base
    belongs_to :catalog
end

Показать страницу

.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
                %ul
                    - @essential.favorites.each do |favorite|
                        %li= favorite.song_title

        .col-md-6
            #catalogs
                %h2 Catalogs
                %table
                    - @essential.catalogs.each do |catalog|
                        %tr
                            %td= catalog.song_name
                            %td= link_to catalog.catalog_url_type, "#{catalog.catalog_url}"
                    - @essential.sub_catalogs.each do |sub_catalog|
                        %tr
                            %td= sub_catalog.sub_url


        .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'

_catalog_fields.html.haml

.form-inline.clearfix
    .row
        .nested-fields
            = f.input :song_name,  placeholder: "Add Catalog Song", label: false, input_html: { class: 'form-input form-control'}
            #sub_catalogs
                = f.simple_fields_for :sub_catalogs do |sub_catalog|
                    = render 'sub_catalog_fields', f: sub_catalog
                .links
                    = link_to_add_association 'add Url', f, :sub_catalogs, class: 'btn btn-secondary add-button'
            = link_to_remove_association "Remove", f, class: 'btn btn-secondary form button'

sub_catalog_fields.html.haml

.form-inline.clearfix
    .row
        .nested-fields
            = f.input :sub_url,  placeholder: "URL", label: false, input_html: { class: 'form-input form-control'}
            = f.input :sub_url_type, collection: ["Youtube", "Spotify", "SoundCloud", "Apple Music", "Google Play"], input_html: { class: "form-control form-input" }
            = link_to_remove_association "Remove", f, class: 'btn btn-secondary form button'

person Taylor    schedule 11.12.2016    source источник


Ответы (1)


Я смог отобразить данные, добавив has_many :sub_catalogs, through: :catalogs, :dependent => :destroy и accepts_nested_attributes_for :sub_catalogs, :reject_if => :all_blank, :allow_destroy => true к моей базовой модели.

person Taylor    schedule 12.12.2016