我正在尝试传递两个隐藏字段和一个答案来创建一个新的答案对象。尽管我可以看到散列中的所有参数,但似乎隐藏的参数没有正确包含在答案中。我确定这只是一件简单的事情,我做错了但却无法弄清楚它在哪里。欣赏时间!
Params Hash:
参数:
{"utf8"=>"✓",
"authenticity_token"=>"VaZC2MCN0rWJnVPIqSTbPF9HvFNBfm5b5nA3H8YenuVte9SMi9Iy+G4fxz9bWhkDNwGVz9jMBVTxT/SuXFCh+A==",
"user_id"=>"2",
"question_id"=>"1",
"answer"=>{"answer"=>"a"},
"commit"=>"Create Answer"}
错误: SQLite3 :: ConstraintException:NOT NULL约束失败:answers.user_id:INSERT INTO“answers”(“created_at”,“updated_at”)VALUES(?,?)
_mc_answer.html.erb(部分我用作多项选择题的表单。)
这应该将question_id和user_id以及用户的回答传递到static_pages控制器并创建一个新的Answer对象。
<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模型
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
答案 0 :(得分:0)
您应该将每个Symbol传递给控制器而不是Hash数组。
def answer_params
params.require(:answer).permit(:answer, :user_id, :question_id)
end
不
def answer_params
params.require(:answer).permit(:answer => [:user_id, :question_id, :answer])
end
因为您的"user_id"=>"2",
和"question_id"=>"1"
不在子哈希中,就像它写成"answer"=>{"answer"=>"a", "question_id"=>"1","user_id"=>"2" },
答案 1 :(得分:0)
您的隐藏字段参数不会进入正确位置的参数哈希值,因为您使用了hidden_field_tag
并且没有指定它们包含在answer
下面哈希中的键。试试这个:
<%= hidden_field_tag "answer[user_id]", @user.id %>