F
F
Fulcrum_292014-03-20 19:57:19
Ruby on Rails
Fulcrum_29, 2014-03-20 19:57:19

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"=>"Сохранить"}

As I understand it, the problem is that the date_select helper
<%= f.date_select :expiration_date , {:use_month_names => ['Январь', 'Февраль', 'Март','Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'], :order => [:day, :month, :year]}%>

splits the date into
"expiration_date(3i)"=>"20",
 "expiration_date(2i)"=>"3",
 "expiration_date(1i)"=>"2016"

and this is not suitable for strong params.
def medicine_params
      params.require(:medicine).permit(:company, :name, :med_type, :price, :batch, :quantity, :expiration_date)
    end

What is the correct way to write medicine_params in this case? Thanks in advance!
P.S.
I have tried
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

1 answer(s)
F
Fulcrum_29, 2014-03-20
@Fulcrum_29

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 question

Ask a Question

731 491 924 answers to any question