Ruby on rails 3.2 在Rails 3中同时从多个记录中添加一个嵌套记录

Ruby on rails 3.2 在Rails 3中同时从多个记录中添加一个嵌套记录,ruby-on-rails-3.2,nested-forms,nested-attributes,Ruby On Rails 3.2,Nested Forms,Nested Attributes,我在这方面已经坚持了几天,虽然我设法找到了一些相关的答案,但似乎没有什么东西能够完全涵盖我需要的两个特性 基本上我有一个“问题”和“答案”模型。我想显示尽可能多的答案字段作为问题,并创建他们使用我的自定义控制器。如果未登录,我还想为答案分配会话id或用户id(这样我就可以在他们注册后确定谁回答了)。 我设法显示了这些字段,但现在它们无法保存,我得到的唯一结果是视图上出现“内部错误”消息,控制台上出现异常: !! Unexpected error while processing request:

我在这方面已经坚持了几天,虽然我设法找到了一些相关的答案,但似乎没有什么东西能够完全涵盖我需要的两个特性

基本上我有一个“问题”和“答案”模型。我想显示尽可能多的答案字段作为问题,并创建他们使用我的自定义控制器。如果未登录,我还想为答案分配会话id或用户id(这样我就可以在他们注册后确定谁回答了)。 我设法显示了这些字段,但现在它们无法保存,我得到的唯一结果是视图上出现“内部错误”消息,控制台上出现异常:

!! Unexpected error while processing request: expected Array (got Rack::Utils::KeySpaceConstrainedParams) for param `question'
这是我的模型/控制器的外观:

step1\u控制器.rb

class Configurator::Step1Controller < ApplicationController
    before_filter :authenticate_user!

  def new
    @questions = Question.includes(:choices).all()
  end

  def create
    Question.update_attributes(params[:question].keys, params[:question].values)
    flash[:notice] = 'Reports were successfully updated.'
    redirect_to root_path
  end

end
class Answer < ActiveRecord::Base
  attr_accessible :weight, :user_id, :question_id
  belongs_to :user
  belongs_to :question
end
class Question < ActiveRecord::Base
  attr_accessible :created_at, :desc, :updated_at, :title, :created_by_id, :updated_by_id, :tag_id, :answers_attributes
  belongs_to :created_by, :class_name => 'User'
  belongs_to :updated_by, :class_name => 'User'
  belongs_to :tag
  has_many :choices
  has_many :answers
  has_many :user, :through => :answers
  accepts_nested_attributes_for :answers
end
类配置器::Step1Controller
模型/答案.rb

class Configurator::Step1Controller < ApplicationController
    before_filter :authenticate_user!

  def new
    @questions = Question.includes(:choices).all()
  end

  def create
    Question.update_attributes(params[:question].keys, params[:question].values)
    flash[:notice] = 'Reports were successfully updated.'
    redirect_to root_path
  end

end
class Answer < ActiveRecord::Base
  attr_accessible :weight, :user_id, :question_id
  belongs_to :user
  belongs_to :question
end
class Question < ActiveRecord::Base
  attr_accessible :created_at, :desc, :updated_at, :title, :created_by_id, :updated_by_id, :tag_id, :answers_attributes
  belongs_to :created_by, :class_name => 'User'
  belongs_to :updated_by, :class_name => 'User'
  belongs_to :tag
  has_many :choices
  has_many :answers
  has_many :user, :through => :answers
  accepts_nested_attributes_for :answers
end
class-Answer
模型/问题.rb

class Configurator::Step1Controller < ApplicationController
    before_filter :authenticate_user!

  def new
    @questions = Question.includes(:choices).all()
  end

  def create
    Question.update_attributes(params[:question].keys, params[:question].values)
    flash[:notice] = 'Reports were successfully updated.'
    redirect_to root_path
  end

end
class Answer < ActiveRecord::Base
  attr_accessible :weight, :user_id, :question_id
  belongs_to :user
  belongs_to :question
end
class Question < ActiveRecord::Base
  attr_accessible :created_at, :desc, :updated_at, :title, :created_by_id, :updated_by_id, :tag_id, :answers_attributes
  belongs_to :created_by, :class_name => 'User'
  belongs_to :updated_by, :class_name => 'User'
  belongs_to :tag
  has_many :choices
  has_many :answers
  has_many :user, :through => :answers
  accepts_nested_attributes_for :answers
end
类问题'User'
属于:更新者:class\u name=>'User'
属于:标签
你有很多选择吗
有很多答案吗
有多个:user,:至=>:答案
接受\u嵌套的\u属性\u用于:答案
结束
routes.rb

class Configurator::Step1Controller < ApplicationController
    before_filter :authenticate_user!

  def new
    @questions = Question.includes(:choices).all()
  end

  def create
    Question.update_attributes(params[:question].keys, params[:question].values)
    flash[:notice] = 'Reports were successfully updated.'
    redirect_to root_path
  end

end
class Answer < ActiveRecord::Base
  attr_accessible :weight, :user_id, :question_id
  belongs_to :user
  belongs_to :question
end
class Question < ActiveRecord::Base
  attr_accessible :created_at, :desc, :updated_at, :title, :created_by_id, :updated_by_id, :tag_id, :answers_attributes
  belongs_to :created_by, :class_name => 'User'
  belongs_to :updated_by, :class_name => 'User'
  belongs_to :tag
  has_many :choices
  has_many :answers
  has_many :user, :through => :answers
  accepts_nested_attributes_for :answers
end
名称空间:配置器do 获取“step1”,:to=>“step1#new”,:as=>:step1 发布“step1”=>“step1#创建”,:as=>:step1 结束

查看

<%= form_for :question, :url => configurator_step1_path do -%>
      <% for question in @questions %>
       <%= fields_for "question[]", question do |question_fields| %>
        <%= question_fields.hidden_field :id %>
        <%= question_fields.label :title, question.title %>
        <%= question_fields.fields_for :answers, [Answer.new] do |li_fields| %>
          <%= li_fields.text_field :weight %>
          <% end %>
        <% end %>
        <% end %>
      <%= submit_tag "Create line items" %>
        <% end %>
       <% if false %>
      <%= f.fields_for :answers, [Answer.new]*5 do |li_fields| %>
      <%= li_fields.label :weight %>
      <%= li_fields.text_field :weight %>
         <% end %>
      <br>
   <% end %>
configurator\u step1\u路径do-%>


您可能想看看这个railscast

也在控制器中,而不是:

Question.update_attributes(params[:question].keys, params[:question].values)
你可以做:

Question.update_attributes(params[:question])

嗨,皮埃尔,谢谢你的回答。我尝试了那一行代码,但就我的立场而言,我仍然遇到同样的问题。我还查看了一些视频,包括嵌套表单以及多个记录同时编辑,但没有一个视频能够同时涵盖这两种场景,这是我的特例。还有其他想法吗?