- 首页
- 其他开发
- Rails 4.0.0 中的 ActiveModel 禁止错误
Rails 4.0.0 中的 ActiveModel 禁止错误
[英] ActiveModel Forbidden Error in Rails 4.0.0
本文介绍了Rails 4.0.0 中的 ActiveModel 禁止错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究嵌套的属性形式.
这是两个模型..员工.rb
class Employee :employeeID, foreign_key: :employeeIDaccepts_nested_attributes_for :employee_info结尾
员工信息.rb
class EmployeeInfo
我的表单在 _form.html.rb
<%= form_for @employee, html: {class: "form form-horizontal validate-form", novalidate: "novalidate"} do |f|%><% if @employee.errors.any?%><div id="error_explanation"><div class="alert alert-danger alert-dismissable"><a class="close" data-dismiss="alert" href="#">×</a><h2><%=pluralize(@employee.errors.count, "error") %>禁止保存此商店:</h2><ul><% @employee.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%>
<%= f.label :employeeID, class: 'col-md-2 control-label' %><div class='col-md-5'><%= f.text_field :employeeID, {class: 'form-control'} %>
<%= f.label :employee_name, class: 'col-md-2 control-label' %><div class='col-md-5'><%= f.text_field :employee_name, class: 'form-control' %>
<%= f.fields_for :employee_info do |ff|%>
<%= ff.label :hired, class: 'col-md-2 control-label' %><div class='col-md-5'><%= ff.text_field :hire_date, class: 'form-control' %>
<%= ff.label :terminated, class: 'col-md-2 control-label' %><div class='col-md-5'><%= ff.text_field :term_date, class: 'form-control' %>
<%结束%>
<%结束%>
和控制器中的更新方法
def 更新response_to do |格式|p "------------------------------"p 员工参数如果@employee.update(employee_params)format.html { redirect_to @employee,注意:'员工已成功更新.'}format.json { 头:no_content }别的format.html { 渲染动作:'编辑' }format.json { 渲染 json:@employee.errors,状态::unprocessable_entity }结尾结尾结尾
控制台中输出的 p 个 employee_params 在哪里
{"employeeID"=>"103", "employee_name"=>"James Michule", "employee_info_attributes"=>{"hire_date"=>"1996-03-12 11:30:00 UTC"、term_date"=>1996-03-12 11:30:00 UTC"、hourly_rate"=>7.4"、address"=>108 E. Jay"、phone_number1""=>"", "phone_number2"=>"", "zipcode"=>"65721", "state"=>"MO", "city"=>"Ozark", "id"=>"30"}}
当我尝试更新时出现错误..错误:
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):app/controllers/employees_controller.rb:56:in `block in update'app/controllers/employees_controller.rb:53:in `update'
怎么了.?请帮忙
解决方案
你的employees_controller
私有定义员工参数params.require(:employee).permit(:term_date, :hire_date)结尾
添加term_date &employee_params
方法中的 hire_date 与上面的代码类似.
希望能解决问题.
附言在 Rails >= 4
上创建应用程序之前,请阅读主题Rails 强参数"Im working on a nested attribute form.
these are the two models..
Employee.rb
class Employee < ActiveRecord::Base
has_one :employee_info, :primary_key => :employeeID, foreign_key: :employeeID
accepts_nested_attributes_for :employee_info
end
EmployeeInfo.rb
class EmployeeInfo < ActiveRecord::Base
belongs_to :employee, primary_key: :employeeID, foreign_key: :employeeID
validates_uniqueness_of :employeeID
end
And i have my form in _form.html.rb
<%= form_for @employee, html: {class: "form form-horizontal validate-form", novalidate: "novalidate"} do |f| %>
<% if @employee.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger alert-dismissable">
<a class="close" data-dismiss="alert" href="#">×</a>
<h2><%= pluralize(@employee.errors.count, "error") %> prohibited this shop from being saved:</h2>
<ul>
<% @employee.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<div class='form-group'>
<%= f.label :employeeID, class: 'col-md-2 control-label' %>
<div class='col-md-5'>
<%= f.text_field :employeeID, {class: 'form-control'} %>
</div>
</div>
<div class='form-group'>
<%= f.label :employee_name, class: 'col-md-2 control-label' %>
<div class='col-md-5'>
<%= f.text_field :employee_name, class: 'form-control' %>
</div>
</div>
<%= f.fields_for :employee_info do |ff| %>
<div class='form-group'>
<%= ff.label :hired, class: 'col-md-2 control-label' %>
<div class='col-md-5'>
<%= ff.text_field :hire_date, class: 'form-control' %>
</div>
</div>
<div class='form-group'>
<%= ff.label :terminated, class: 'col-md-2 control-label' %>
<div class='col-md-5'>
<%= ff.text_field :term_date, class: 'form-control' %>
</div>
</div>
<% end %>
<div class='form-actions form-actions-padding-sm'>
<div class='row'>
<div class='col-md-10 col-md-offset-2'><i class='icon-save custom-icon'></i>
<% if params[:action] == "new" %>
<%= f.submit "Create", class: 'btn btn-primary custom-button' %>
<% else %>
<%= f.submit "Update", class: 'btn btn-primary custom-button' %>
<% end %>
<%= link_to 'Cancel', shops_path, class: 'btn' %>
</div>
</div>
</div>
<% end %>
and the update method in controller
def update
respond_to do |format|
p "------------------------------"
p employee_params
if @employee.update(employee_params)
format.html { redirect_to @employee, notice: 'Employee was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @employee.errors, status: :unprocessable_entity }
end
end
end
where that p employee_params output in console is
{"employeeID"=>"103", "employee_name"=>"James Michule", "employee_info_attributes"=>{"hire_date"=>"1996-03-12 11:30:00 UTC", "term_date"=>"1996-03-12 11:30:00 UTC", "hourly_rate"=>"7.4", "address"=>"108 E. Jay", "phone_number1"=>"", "phone_number2"=>"", "zipcode"=>"65721", "state"=>"MO", "city"=>"Ozark", "id"=>"30"}}
and when i try to update i get an error..
error:
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
app/controllers/employees_controller.rb:56:in `block in update'
app/controllers/employees_controller.rb:53:in `update'
What is wrong.? Please help
解决方案
Your employees_controller
private
def employee_params
params.require(:employee).permit(:term_date, :hire_date)
end
Add term_date & hire_date in the employee_params
method like the code above.
Hope it solves the issue.
P.S. And please read the topic "Rails Strong parameters" before creating the app on rails >= 4
这篇关于Rails 4.0.0 中的 ActiveModel 禁止错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文