How to handle multiple models in one rails form?

后端 未结 2 905
自闭症患者
自闭症患者 2020-12-31 11:25

I have the following models

class Survey < ActiveRecord::Base
  has_many :survey_sections
  accepts_nested_attributes_for :survey_sections
end

class Surv         


        
相关标签:
2条回答
  • 2020-12-31 11:54

    You can handle as many forms as you need, if you use the fields_for helper properly.

    This is where you're falling short I think (your controller seems okay).

    I also wrote an answer about this some time back.

    #app/models/survey.rb
    class Survey < ActiveRecord::Base
        has_many :sections
        accepts_nested_attributes_for :sections
    end
    
    #app/models/section.rb
    class Section < ActiveRecord::Base
        belongs_to :survey
        has_many :questions
        accepts_nested_attributes_for :questions
    end
    
    #app/models/question.rb
    class Question < ActiveRecord::Base
        belongs_to :section
        has_many :answers
    end
    

    Try and keep your model names as succinct as possible.

    #app/controllers/surveys_controller.rb
    class SurveysController < ApplicationController
       def new
          @survey = Survey.new
          @survey.sections.build.questions.build
       end
    
       def create
          @survey = Survey.new survey_params
          @survey.save
       end
    
       private 
    
       def survey_params
          params.require(:survey).permit(:title, sections_attributes: [:title, questions_attributes:[:title]])
       end
    end
    
    #app/views/surveys/new.html.erb
    <%= form_for @survey do |f| %>
       <%= f.text_field :title %>
       <%= f.fields_for :sections do |section| %>
           <%= section.text_field :title %>
           <%= section.fields_for :questions do |question| %>
               <%= question.text_field :title %>
           <% end %>
       <% end %> 
       <%= f.submit %>
    <% end %>
    
    0 讨论(0)
  • 2020-12-31 12:16

    You can get best explanation here with same type of model

    http://railscasts.com/episodes/196-nested-model-form-part-1

    #app/models/survey.rb
    class Survey < ActiveRecord::Base
        has_many :sections, :dependent => :destroy
        accepts_nested_attributes_for :sections, :allow_destroy => true
    end
    
    #app/models/section.rb
    class Section < ActiveRecord::Base
        belongs_to :survey
        has_many :questions, :dependent => :destroy
        accepts_nested_attributes_for :questions, :allow_destroy => true
    end
    
    #app/models/question.rb
    class Question < ActiveRecord::Base
        belongs_to :section
        has_many :answers
    end
    

    now in controller

    def new
      @survey = Survey.new
      section = @survey.sections.build
      section.questions.build 
      end
    end
    

    Now in views

    <%= form_for @survey do |f| %>
      <%= f.error_messages %>
      <p>
        <%= f.label :name %><br />
        <%= f.text_field :name %>
      </p>
      <%= f.fields_for :sections do |builder| %>
         <%= builder.text_field :title %>
         <%= builder.fields_for :questions do |question| %>
            <%=  question.text_field :content%>
         <% end %>
      <% end %>
      <p><%= f.submit "Submit" %></p>
    <% end %>
    
    0 讨论(0)
提交回复
热议问题