Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3.2: Trying to pass User field through nested form
    primarykey
    data
    text
    <p>So based on Ryan Bates rails cast (http://railscasts.com/episodes/196-nested-model-form-revised) I am creating a nested form. The part of the rails application that I am trying to get to work ideally does the following:</p> <ul> <li>Let a current user ask a question</li> <li>Then provide an answer for that question afterwards</li> </ul> <p>I managed to get everything working, except that when I try to submit the form once everything is filled out, I keep getting the following error: </p> <pre><code>undefined method `meter_id' for nil:NilClass app/models/answer.rb:13:in `associate_with_meter_id' app/controllers/questions_controller.rb:13:in `create' </code></pre> <p>I believe I know what is wrong, but I am not sure how to fix it. The meter_id is returning an undefined value, because it is not being passed the correct value. Here is the method that associates the meter_id (of answers) with the meter_id (of users):</p> <pre><code>def associate_with_meter_id self.meter_id = user.meter_id end </code></pre> <p><strong>Here is a partial of my user model</strong> </p> <pre><code>class User &lt; ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me, :home_size_sf, :meter_id, :avg_monthly_kwh, :discovery_score, :questions_attributes, :answers_attributes has_many :data_records, :foreign_key =&gt; :meter_id, :primary_key =&gt; :meter_id, :class_name =&gt; "DataRecord" has_many :questions has_many :answers accepts_nested_attributes_for :questions, :answers </code></pre> <p><strong>Here is the questions model</strong></p> <pre><code>class Question &lt; ActiveRecord::Base attr_accessible :description, :taxonomy, :user_id, :answers_attributes belongs_to :user has_many :answers accepts_nested_attributes_for :answers validates :description, presence: { :on =&gt; :create } validates :taxonomy, presence: { :on =&gt; :create } def relevance_score rand end end </code></pre> <p><strong>Here is the questions controller</strong></p> <pre><code>class QuestionsController &lt; ApplicationController respond_to :html, :json def index @question = current_user.questions.new @questions = current_user.questions.all end def create @question = current_user.questions.new(params[:question]) if !params[:update_button] if @question.valid? if params[:next_button] || !@question.save render 'index' elsif !params[:next_button] &amp;&amp; params[:submit_button] &amp;&amp; @question.save flash[:success] = "Your question and answer have been saved." respond_with @question, :location =&gt; questions_path end else render 'index' end else render 'index' end end def next @question = current_user.unanswered.first @answer = Answer.new(:question =&gt; @question, :user =&gt; current_user) respond_to do |format| format.js end end end </code></pre> <p><strong>answers model</strong></p> <pre><code>class Answer &lt; ActiveRecord::Base attr_accessible :value, :user_id, :meter_id, :question_id belongs_to :user belongs_to :question validates :value, presence: true, :numericality =&gt; true before_save :associate_with_meter_id def associate_with_meter_id self.meter_id = user.meter_id **(&lt;-- line 13 from the error message)** end end </code></pre> <p><strong>answers controller</strong></p> <pre><code>class AnswersController &lt; ApplicationController respond_to :html, :json def index @answers = current_user.answers end def create @answer = current_user.answers.create(params[:answer]) if @answer.save flash[:notice] = "Thanks for for answer. Please continue with your input...." respond_with @answer, :location =&gt; root_url end end end </code></pre> <p><strong>database schema</strong></p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20120210184340) do create_table "answers", :force =&gt; true do |t| t.integer "meter_id" t.integer "user_id" t.integer "question_id" t.float "value" t.float "what_if_value" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end create_table "data_records", :force =&gt; true do |t| t.datetime "timestamp" t.float "value" t.integer "meter_id" t.string "status_code" end create_table "questions", :force =&gt; true do |t| t.string "description" t.string "taxonomy" t.string "coeff" t.float "rsquare", :default =&gt; 0.0 t.string "rank" t.string "responses" t.string "skips" t.string "avganswer" t.float "pval", :default =&gt; 0.0 t.float "quality", :default =&gt; 0.0 t.integer "user_id" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end create_table "setup_constants", :force =&gt; true do |t| t.float "exp_model", :default =&gt; 0.0 t.float "exp_pval_const", :default =&gt; 0.0 end create_table "users", :force =&gt; true do |t| t.integer "meter_id" t.float "home_size_sf", :default =&gt; 1000.0 t.text "notifications" t.float "avg_monthly_kwh" t.float "ee_score" t.string "email", :default =&gt; "", :null =&gt; false t.string "encrypted_password", :default =&gt; "", :null =&gt; false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", :default =&gt; 0 t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end add_index "users", ["email"], :name =&gt; "index_users_on_email", :unique =&gt; true add_index "users", ["reset_password_token"], :name =&gt; "index_users_on_reset_password_token", :unique =&gt; true end </code></pre> <p><strong>Note:</strong></p> <p>In the answers model (above), the line:</p> <pre><code>self.meter_id = user.meter_id </code></pre> <p>Associates the meter_id of the answer model with the meter_id of the user model. I believe this is where the issue is. I tried changing the above line to:</p> <pre><code>self.meter_id = 2 </code></pre> <p>And then everything worked fine, so it's obvious that the user.meter_id is undefined, so I'm not sure how to pass that value through the nested form? I tried using a hidden field but with no luck (the following is a nested fields_for :answers, within a form_for @questions):</p> <pre><code>&lt;fieldset&gt; &lt;%= f.label "Yes" %&gt; &lt;%= f.radio_button :value, 1 %&gt; &lt;%= f.label "No" %&gt; &lt;%= f.radio_button :value, 0 %&gt; &lt;%= f.hidden_field :user_id %&gt; &lt;%= f.hidden_field :question_id %&gt; &lt;%= f.hidden_field :meter_id %&gt; &lt;/fieldset&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.
 

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