如何在Rails中保存嵌套表单数据?

时间:2015-07-18 03:32:34

标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord nested-forms

我的食物和饮料表中有这些数据:

食物表

ID  | Name
------------
1   | Rojak
2   | Lemang

饮料表

ID  | Name
------------
1   | Barli
2   | Sirap

我的模特关系是:

class Survey < ActiveRecord::Base
  has_many :questions
  accepts_nested_attributes_for :questions, allow_destroy: true
end
class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :answers
  accepts_nested_attributes_for :answers, allow_destroy: true
end
class Answer < ActiveRecord::Base
  belongs_to :question
  has_many :foods
  has_many :drinks
  accepts_nested_attributes_for :foods, :drinks
end
class Food < ActiveRecord::Base
    belongs_to :answer
end
class Drink < ActiveRecord::Base
    belongs_to :answer
end

这是app / views / surveys中的 _form.html.erb 文件:

<%= form_for @survey do |f| %>
  <% if @survey.errors.any? %>
    <div class="error_messages">
      <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
      <ul>
      <% @survey.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>

  </div>
  <br>
  <%= f.fields_for :questions do |f| %>
    <fieldset>
    <%= f.label :content, "Question" %><br />
    <%= f.text_area :content %><br />

    <% Food.all.each do |fd| %>
    <fieldset>
      <%= f.fields_for :answers do |f| %>
        <%= f.text_field :name, :value => fd.name %>
        <%= f.number_field :quantity %>
      <% end %> <br>
    </fieldset>
    <% end %>

  </fieldset>
  <br>
  <% end %>
  <br>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我要做的是:

  1. 列出食品和饮料领域内的所有食品name和饮料name
  2. 给每个食物和饮料quantity(此quantity列在答案表内)
  3. 保存并更新
  4. 但我没有运气。当我尝试加载编辑视图(表单)时,我设法列出了所有的食物名称。但是,我不知道如何在自己的字段集中列出所有饮品名称。

    我试着这样做,但它不起作用:

    <% if :content == "Foods" %>
      <%= #loads foods %>
    <% else %>
      <%= #loads drinks %>
    <% end %>
    

    而且,当我试图保存食品/饮料的名称和数量时,它也不起作用。

    演示:

    enter image description here

    如何解决这个问题?

    注意:

    这是survey_controller中的更新方法:

    def update
        if @survey.update(survey_params)
          redirect_to @survey, notice: "Successfully updated survey."
        else
          render :edit
        end
      end
    
    ...
    
    def survey_params
        params.require(:survey).permit(:name, questions_attributes: [:id, :_destroy, :content, answers_attributes: [:id, :_destroy, :content, :quantity]])
      end
    

    更新

    这是我的show.html.erb

    <h1><%= @survey.name %></h1>
    
    <ul class="questions">
    <% @survey.questions.each do |question| %>
      <li>
        <%= question.content %>
        <ol class="answers">
          <% question.answers.each do |answer| %>
            <li><%= answer.content %> (<%= answer.quantity %>)</li>
          <% end %>
        </ol>
      </li> <br>
    <% end %>
    </ul>
    
    <%= link_to 'Edit', edit_survey_path(@survey) %> |
    <%= link_to 'Back to Surveys', surveys_path %>
    

1 个答案:

答案 0 :(得分:2)

您可能会看到变量范围的问题。每次使用f.fields_for创建嵌套表单时,都会传入变量f,即使f是父项中使用的变量。

尝试将其与问题和答案区别开来,例如:

<%= f.fields_for :questions do |fq| %>
  <fieldset>
    <%= fq.label :content, "Question" %><br />
    <%= fq.text_area :content %><br />

    <% Food.all.each do |fd| %>
    <fieldset>
      <%= fq.fields_for :answers do |fa| %>
      <%= fa.text_field :name, :value => fd.name %>
      <%= fa.number_field :quantity %>
    <% end %> <br>
  </fieldset>
  <% end %>

</fieldset>
<br>
<% end %>