Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd one nested record from several records at once in Rails 3
    primarykey
    data
    text
    <p>I've been stuck for a few days on this and although I managed to find a few relevant answers, nothing seems to cover the two features I need altogether.</p> <p>Basically I've got a "Question", and "Answer" model. I want to show as many answer fields as questions are there, and create them using my custom controller. I also want to assign either session id if not signed in, or user Id to the answer (so then I can work out who answered once they register). I managed to show the fields, but now they won't save, only thing I get is an "internal error" message on the view and an exception on the console:</p> <pre><code>!! Unexpected error while processing request: expected Array (got Rack::Utils::KeySpaceConstrainedParams) for param `question' </code></pre> <p>This are what my models / controller look like:</p> <p><strong>step1_controller.rb</strong></p> <pre><code>class Configurator::Step1Controller &lt; 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 </code></pre> <p><strong>models/answer.rb</strong></p> <pre><code>class Answer &lt; ActiveRecord::Base attr_accessible :weight, :user_id, :question_id belongs_to :user belongs_to :question end </code></pre> <p><strong>models/question.rb</strong></p> <pre><code>class Question &lt; 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 =&gt; 'User' belongs_to :updated_by, :class_name =&gt; 'User' belongs_to :tag has_many :choices has_many :answers has_many :user, :through =&gt; :answers accepts_nested_attributes_for :answers end </code></pre> <p><strong>routes.rb</strong></p> <p>namespace :configurator do get "step1", :to => 'step1#new', :as => :step1 post "step1" => "step1#create", :as => :step1 end</p> <p><strong>view</strong></p> <pre><code>&lt;%= form_for :question, :url =&gt; configurator_step1_path do -%&gt; &lt;% for question in @questions %&gt; &lt;%= fields_for "question[]", question do |question_fields| %&gt; &lt;%= question_fields.hidden_field :id %&gt; &lt;%= question_fields.label :title, question.title %&gt; &lt;%= question_fields.fields_for :answers, [Answer.new] do |li_fields| %&gt; &lt;%= li_fields.text_field :weight %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= submit_tag "Create line items" %&gt; &lt;% end %&gt; &lt;% if false %&gt; &lt;%= f.fields_for :answers, [Answer.new]*5 do |li_fields| %&gt; &lt;%= li_fields.label :weight %&gt; &lt;%= li_fields.text_field :weight %&gt; &lt;% end %&gt; &lt;br&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload