我创建了这个表格:
<%= form_for @task do |task| %>
<%= task.hidden_field :user_id, :value => @user.id %>
<%= task.label :task %>
<%= task.text_area :task %>
<%= task.label :deadline %>
<%= task.text_field :deadline %>
<%= task.submit "Add" %>
<% end %>
和控制器:
def new
@task = Todo.new
end
def create
@task = Todo.new(params[:task])
if @task.save
set_flash "Task added"
redirect_to action: 'index'
else
set_flash "Task adding failed, Please try again"
redirect_to action: 'index'
end
end
def set_user
@user = current_user
end
当我提交表单时,我收到此错误:
SQLite3::ConstraintException: todos.user_id 可能不是 NULL: INSERT INTO "todos" ("completed", "created_at", "deadline", "task", "updated_at", "user_id") VALUES (?, ?, ? , ?, ?, ?)
问题是什么?
编辑:
这是rails传递的参数:
{"utf8"=>"✓",
"authenticity_token"=>"oJr4ii4szyBVAacBJDNYcZn8QWGrFij4dF1wqD1Fvic=",
"todo"=>{"user_id"=>"1",
"task"=>"Hello World",
"deadline"=>"03/20/2013"},
"commit"=>"Add"}
这是模型:
attr_accessible :user_id, :deadline, :task, :completed
谢谢