Answer the question
In order to leave comments, you need to log in
Rails 4 strong parameters ForbiddenAttributesError - how to fix?
I can't find a solution for the following problem:
I have a Medicine model with an expiration_date attribute of type Date. When I try to create a new Medicine record, I get the following error
ActiveModel::ForbiddenAttributesError
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"5cxCW7RfAr/DrdWd/PfxN093Zj41KqlVBUWxyK2LZvE=",
"medicine"=>{"company"=>"Company",
"name"=>"Name",
"med_type"=>"Category",
"price"=>"100",
"batch"=>"AB12345",
"expiration_date(3i)"=>"20",
"expiration_date(2i)"=>"3",
"expiration_date(1i)"=>"2016",
"quantity"=>"10"},
"commit"=>"Сохранить"}
<%= f.date_select :expiration_date , {:use_month_names => ['Январь', 'Февраль', 'Март','Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'], :order => [:day, :month, :year]}%>
"expiration_date(3i)"=>"20",
"expiration_date(2i)"=>"3",
"expiration_date(1i)"=>"2016"
def medicine_params
params.require(:medicine).permit(:company, :name, :med_type, :price, :batch, :quantity, :expiration_date)
end
params.require(:medicine).permit( :"expiration_date(1i)", :"expiration_date(2i)", :"expiration_date(3i)", :company, :name, :med_type, :price, :batch, :quantity)
the error remains.
Answer the question
In order to leave comments, you need to log in
I found a bug, it is in the filter_resource_access in the medicines controller. It's strange that it doesn't work correctly for the new and create actions, because I'm trying to perform these actions from the admin role. Although index, update and destroy work fine.
authorization_rules.rb
authorization do
role :admin do
has_permission_on [:medicines], :to => [:index, :show, :new, :create, :edit, :update, :destroy]
end
role :guest do
has_permission_on [:medicines], :to => [:show]
has_permission_on [:users], :to => [:index, :show, :new, :create, :edit, :update]
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question