Prkl8r
Prkl8r

Reputation: 19

Rails hidden params not passing correctly

I am trying to pass two hidden fields and an answer to create a new answer object. Even though I can see all of the params in the hash, it seems like the hidden params aren't being included correctly with the answer. I'm sure it's just a simple thing I'm doing wrong but can't figure out where it is. Appreciate the time to look!

Params Hash:

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"VaZC2MCN0rWJnVPIqSTbPF9HvFNBfm5b5nA3H8YenuVte9SMi9Iy+G4fxz9bWhkDNwGVz9jMBVTxT/SuXFCh+A==",
 "user_id"=>"2",
 "question_id"=>"1",
 "answer"=>{"answer"=>"a"},
 "commit"=>"Create Answer"}

ERROR: SQLite3::ConstraintException: NOT NULL constraint failed: answers.user_id: INSERT INTO "answers" ("created_at", "updated_at") VALUES (?, ?)

_mc_answer.html.erb (Partial I am using as a form for multiple choice questions.)

This should pass the question_id and user_id along with the users' answer into the static_pages controller and create a new Answer object.

<h4>MC Answer:</h4>
  <p>User ID: <%= @user.id %></p>
  <p>Question ID: <%= question_id %></p>
  <%= form_for(@new_answer, url: static_pages_path(@new_answer)) do |a| %>
    <%= hidden_field_tag :user_id, @user.id %>
    <%= hidden_field_tag :question_id, question_id %>
    <%= a.text_field :answer %>
    <%= a.submit %>
  <% end %>

static_pages_controller.rb

class StaticPagesController < ApplicationController

  def index
    @user = current_user

    if user_signed_in?  
      if @user.manager_id != nil
        @manager = @user.manager_id
      end
      @my_team = User.where("manager_id = ? AND id != ?", @manager, @user.id)
      @questions = Question.active_question.where.not(id: @user.answers)     
    end
    @new_answer = Answer.new
  end

  def new
    @new_answer = Answer.new
  end

  def create
    @user = current user ## Tried adding this to the create method per suggestion below but still getting the same issue. 
    @new_answer = Answer.new(answer_params)
    if @new_answer.save!
      flash.now[success]
    else
    end  
  end

  private


  ## Edited params from .permit(:answer => [:user_id, :question_id, :answer]) but still getting similar error.
  def answer_params
    params.require(:answer).permit(:user_id, :question_id, :answer)
  end
end

answer.rb model

class Answer < ActiveRecord::Base


  belongs_to :question
  belongs_to :user

  scope :user_answered, lambda {|q| where("question_id == (?)", q) }


  def next
    Answer.where("id > ?", id).limit(1).first
  end

  def prev
    Answer.where("id < ?", id).limit(1).first
  end  
end



DB Schema for answer model if that's of any use: 

ActiveRecord::Schema.define(version: 20151112025039) do

  create_table "answers", force: :cascade do |t|
    t.integer  "user_id",       null: false
    t.integer  "question_id",   null: false
    t.integer  "points_earned"
    t.text     "comment"
    t.text     "answer",        null: false
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
  end


  add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  add_index "users", ["unlock_token"], name: "index_users_on_unlock_token", unique: true

end

Upvotes: 0

Views: 1319

Answers (2)

Jason
Jason

Reputation: 2743

Your hidden field parameters aren't going in to the parameters hash in the right spot because you used hidden_field_tag and didn't specify that they were to be included under the answer key in the hash. Try this:

<%= hidden_field_tag "answer[user_id]", @user.id %>

Upvotes: 0

Papouche Guinslyzinho
Papouche Guinslyzinho

Reputation: 5448

You should pass each Symbol to the controller not an array of Hash.

  def answer_params
    params.require(:answer).permit(:answer, :user_id, :question_id)
  end 

not

 def answer_params
    params.require(:answer).permit(:answer => [:user_id, :question_id, :answer])
  end

Because that your "user_id"=>"2", and "question_id"=>"1" are not in a sub-Hash like if it was written "answer"=>{"answer"=>"a", "question_id"=>"1","user_id"=>"2" },

Upvotes: 0

Related Questions