【发布时间】:2013-03-21 18:07:00
【问题描述】:
我创建了这个表单:
<%= 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
谢谢
【问题讨论】:
标签: ruby-on-rails forms sqlite activerecord