1
我想在使用form_for帮助器方法的轨道中创建一个新的记录。我认为params散列是空白的,因为当我提交表单时,我一直收到空白的错误。使用参数散列保存在轨道4.0中的记录
这是我的形式:
<% provide(:title, "Add Department") %>
<h1>Add Department</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@department) do |f| %>
<%= render 'shared/department_error_messages' %>
<%= f.label :Full_Department_Title %>
<%= f.text_field :full_name %>
<%= f.label :Department_Abbreviation %>
<%= f.text_field :abbreviation %>
<%= f.submit "Add department", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
这是我的部门控制器
class DepartmentsController < ApplicationController
def show
@department = Department.find(params[:id])
end
def new
@department = Department.new
end
def create
@department = Department.new(params[department_params]) # Not the final implementation!
if @department.save
redirect_to root_path
else
render 'new'
end
end
private
def department_params
# This says that params[:department] is required, but inside that, only params[:department][:full_name] and
# params[:department][:abbreviation] are permitted. Unpermitted params will be stripped out
params.require(:department).permit(:full_name, :abbreviation)
end
end
这是型号:
class Department < ActiveRecord::Base
validates :full_name, presence: true, length: { minimum: 6 }
end
当我提交,错误被渲染称FULL_NAME不能为空(并且字段现在为空)。调试信息是:
--- !ruby/hash:ActionController::Parameters utf8: ✓
authenticity_token: EjfYjWAzaw7YqVZAkCPZwiEMFfb2YLIRrHbH1CpZOwA=
department: !ruby/hash:ActionController::Parameters
full_name: Sports Department
abbreviation: SD
commit: Add department
action: create
controller: departments
我也检查了开发日志。交易开始并被回滚。我可以在控制台中保存一条记录,所以我猜测它与params散列有关,但无法弄清楚。请帮助
非常感谢!菜鸟错误。 – brownie3003 2013-04-24 21:22:24