【问题标题】:Rails 4 - Nested form with accepts_nested_attributes_for controller setup?Rails 4-带有accepts_nested_attributes_for控制器设置的嵌套表单?
【发布时间】:2014-12-29 03:12:27
【问题描述】:

我正在尝试使用form_forfields_for 制作嵌套表单。经过大量研究和成功,不再从事我的项目。我只是想重新创建一个 railscast 看看我做错了什么。

我正在尝试重新创建在http://railscasts.com/episodes/196-nested-model-form-part-1 找到的示例,因为代码在那里,所以应该不会那么难,但我无法从调查中创建问题。到目前为止,这是我的代码:

rails new surveysays
rails g scaffold survey name:string
rake db:migrate
rails g scaffold question survey_id:integer content:text
rake db:migrate

我正在尝试以完全相同的视频顺序进行操作。 我的问题模型:

class Question < ActiveRecord::Base
  belongs_to :survey
end

我的调查模型:

class Survey < ActiveRecord::Base
  has_many :questions, dependent: :destroy
  accepts_nested_attributes_for :questions
end

我的带有嵌套问题字段的调查表:

<%= form_for(@survey) do |f| %>
      ...
      <div class="field">
        <%= f.label :name %><br>
        <%= f.text_field :name %>
      </div>
      <%= f.fields_for :questions do |builder| %>
        <p>
          <%= builder.label :content, "Question" %><br/>
          <%= builder.text_area :content, :row => 3 %>
        </p>
      <% end %>
      <div class="actions">
        <%= f.submit %>
      </div>
   <% end %>

我的调查显示:

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @survey.name %>
</p>

<ol>
  <% for question in @survey.questions %>
   <li><%=h question.content%></li>
  <% end %>
</ol>

<%= link_to 'Edit', edit_survey_path(@survey) %> |
<%= link_to 'Back', surveys_path %>

还有我的 SurveysController:

class SurveysController < ApplicationController
 ...

  # GET /surveys/new
  def new
    @survey = Survey.new
    3.times { @survey.questions.build }
  end

 ...

  # POST /surveys
  # POST /surveys.json
  def create
    @survey = Survey.new(survey_params)

    respond_to do |format|
      if @survey.save
        format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
        format.json { render :show, status: :created, location: @survey }
      else
        format.html { render :new }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  ...

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_survey
       @survey = Survey.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def survey_params
       params.require(:survey).permit(:name)
    end
end

直到 5:34 分钟,当它无法正常工作(如视频所示)并且没有创建问题时,表格出现了 3 个问题,我填写了表格,但是当按下 create 它不会创建问题:

加载开发环境(Rails 4.1.6) 2.1.3 :001 > s = Survey.all 调查负载 (3.0ms) 选择“调查”。* FROM“调查” => #]> 2.1.3 :002 > q = s[0].问题 问题加载(0.6 毫秒) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ? [[“survey_id”,2]] => #

我看不出我的代码和示例之间有什么区别。我什至尝试对SurveysController 进行一些更改,但没有成功:

在方法survey_params的允许中插入 question_attributes:[:id ,:content] 要么 在 create 方法的 ifsurvey.save 之后插入 @survey.questions.create(survey_params[:questions_attributes]) ,这会创建问题但内容为:nill

此时我被卡住了。我不知道还能做什么,我在控制器中缺少什么? 谁能给我一些帮助,谢谢。

【问题讨论】:

    标签: ruby-on-rails-4 controller nested-forms nested-attributes


    【解决方案1】:

    在控制器中的 survey_params 方法上,您缺少问题参数,它应该如下所示:

    def survey_params
     params.require(:survey).permit(:name, :questions_attributes => [:question, :answer ... or whatever attribute for the question model])
    end
    

    告诉我进展如何!

    【讨论】:

    • 非常感谢,是的,我之前找到了它,我实际上是来这里回答我的帖子的。感谢您的帮助
    【解决方案2】:

    survey_params 需要更改以允许嵌套属性,来自:

    def survey_params
       params.require(:survey).permit(:name)
    end
    

    到:

    def survey_params
      params.require(:survey).permit(:name, questions_attributes: [:id , :content])
    end
    

    【讨论】:

    • 如果还需要破坏关联,questions_attributes: [:id , :content, :_destroy] 可能更合适。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2017-02-12
    • 2011-10-07
    相关资源
    最近更新 更多