嵌套表单未发布到两个表

时间:2014-04-17 20:31:30

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

我的Ruby on Rails 4表单创建了一个问题。我还想添加创建问题的4个答案的能力。它正在创造问题,但没有答案。它没有发布到答案表,只有问题表。

如何将其发布到答案表的答案?

我的日志POST:

Started POST "/questions" for x at 2014-04-17 13:16:21 -0700
Processing by QuestionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x", "question"=>{"content"=>"Test question 2", "question_type"=>"MC", "category"=>"i
p_voice", "product_id"=>"1", "active"=>"0", "answers_attributes"=>{"0"=>{"content"=>"one piggy", "correct"=>"1", "question_id"=>""}, "1"=>{"content"=>"two elephants", "correct"=>
"0", "question_id"=>""}, "2"=>{"content"=>"one lion", "correct"=>"0", "question_id"=>""}, "3"=>{"content"=>"six oranges", "correct"=>"0", "question_id"=>""}}}, "commit"=>"Create 
Question"}
Unpermitted parameters: answers_attributes
(0.1ms)  begin transaction
SQL (0.3ms)  INSERT INTO "questions" ("active", "category", "content", "created_at", "product_id", "question_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["active", fals
e], ["category", "ip_voice"], ["content", "Test question 2"], ["created_at", Thu, 17 Apr 2014 20:16:21 UTC +00:00], ["product_id", 1], ["question_type", "MC"], ["updated_at", Thu
, 17 Apr 2014 20:16:21 UTC +00:00]]
(31.5ms)  commit transaction

我的questions_controller

def new
@question = Question.new
4.times { @question.answers.build }

@products = Product.all
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def question_params
  params.require(:question).permit(:content, :question_type, :category, :product_id, :active)
end

# Before filters
def signed_in_user
    unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
    end
end

我的answers_controller

def new
@answer = Answer.new
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def answer_params
  params.require(:answer).permit(:content, :question_id, :correct)
end

# Before filters
def signed_in_user
    unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
    end
end

我的问题.rb模型

class Question < ActiveRecord::Base
has_many :answers, dependent: :destroy
belongs_to :user
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:contecnt].blank? }, :allow_destroy => true

我的回答.rb模型

belongs_to :question    

我的表格!

<h1>New question</h1>
<%= form_for(@question) do |f| %>
<%= render 'shared/error_questions' %>
<%= render 'form', f: f %>
<h1>Answers</h1>
<%= f.fields_for :answers do |builder| %>
<%= render 'four_answers', :f => builder %>
<% end %>
    <%= f.submit "Create Question", class: "btn btn-lg btn-primary" %>
<% end %>

1 个答案:

答案 0 :(得分:1)

accepts_nested_attributes_for选项似乎总是被reject_if拒绝。您的a[:contectnt].blank?始终为true,因为哈希中不存在键contectnt

使用:reject_if => lambda { |a| a[:content].blank? },如下所示:

accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

更新,我想是准确答案!:

由于日志显示您不允许answers_attributes进行批量分配,因此您还需要将answers_attributes添加到permit列表,如下所示:

# app/controllers/questions_controller.rb
def question_params
  params.require(:question).permit(:question_type, :category, :product_id, :active, answers_attributes: [ :content, :correct, :question_Id ] )
end

有关详细信息,请参阅edgeapi的Strong Parameters