0

I'm on Rails 4 and using Active Admin. I need to have a different set of parameters permissible for the create and update methods, so I am approaching this by modifying the instructions from the Active Admin documentation. Here is what I am trying to do:

My model needs to take the following set of parameters on create:

:name, :region, :contact_details, :province_id, :status_id, :start_date

But is should not change :region on update. So, without overriding the default Active Admin’s update method, I am modifying permit_params as follows:

  permit_params do
    params = [:name, :contact_details, :province_id, :status_id, :start_date]
    params.push(:region) unless params[:action] == "update"
    params
  end

The result is inevitably the following error:

TypeError: no implicit conversion of Symbol into Integer

which occurs, I believe, when permit_params is creating the method permitted_params.

What am I doing wrong? What is the solution?

azangru
  • 2,644
  • 5
  • 34
  • 55
  • Show code where you use `permit_params` method – Nermin Dec 10 '14 at 10:25
  • `permit_params` are used by ActiveAdmin behind the scenes in its default controllers (please see [here](https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md)). I have not written any code that uses `permit_params`. – azangru Dec 10 '14 at 10:32

3 Answers3

2

A shorter version of Andrey Deineko

 permit_params do
    params = [:region, :name, :contact_details, :province_id, :status_id, :start_date]
    params.delete(:region) if action_name == 'update'
    params
  end

console output:

2.1.5 :021 > params = [:region, :name, :contact_details, :province_id, :status_id, :start_date]
 => [:region, :name, :contact_details, :province_id, :status_id, :start_date] 
2.1.5 :022 > params.delete(:region)
 => :region 
2.1.5 :023 > params
 => [:name, :contact_details, :province_id, :status_id, :start_date] 
rmagnum2002
  • 11,341
  • 8
  • 49
  • 86
  • Short and sweet indeed. Now I have another problem: although your code works and `:region` is listed among unpermitted parameters during an update method, its value still gets updated. But this probably deserves a question of its own. – azangru Dec 10 '14 at 11:59
  • try this `params.delete(:region) if params[:action] == "update"`, might be that `action_name` is not available at that point, but params are sent anyway and params[:action] might work. – rmagnum2002 Dec 10 '14 at 12:54
  • `action_name` is available, thanks for that hint. It was just me being stupid: I was filtering out one parameter and testing another :-) – azangru Dec 10 '14 at 13:32
  • Actually, `params.delete(:region) if params[:action] == "update"` was producing the same `TypeError: no implicit conversion of Symbol into Integer`, while `params.delete(:region) if action_name == 'update'` worked fine. Mysterious. – azangru Dec 10 '14 at 13:34
  • Cool then, something new for me anyway. Might need this at some point. Cheers. – rmagnum2002 Dec 10 '14 at 13:41
1

What if you refactor your code as follows:

 permit_params do
    regular_params = [:name, :contact_details, :province_id, :status_id, :start_date]
    update_params = regular_params + [:region]
    permitted = params[:action] == "update" ? update_params : regular_params
    permitted
  end
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • You cannot use `#push` as that'd modify the original as well. Something like `regular_params + [:region]` should work. – Jiří Pospíšil Dec 10 '14 at 10:42
  • Yes, this works, thank you. I eventually went with rmagnum2002's variation, and now am perplexed by the fact that although I pass the correct set of parameters to the update method, the deleted parameter (:region) still gets updated. – azangru Dec 10 '14 at 12:02
  • @Andrey Deineko: Yes, I have. The shortened list of parameters that is passed to the update method is correct, but for some weird reason the unpermitted parameter still gets updated. I'll need to go deeper. – azangru Dec 10 '14 at 12:28
0

Try this way, maybe your problem is not with permit params

  permit_params :name, :contact_details, :province_id, :status_id, :start_date
  # it does not work as you want but maybe can help you find bug
Nermin
  • 6,118
  • 13
  • 23
  • And what about :region ? He said he don't want :region to be permitted on update. In your case they are permitted in both create and update actions – rmagnum2002 Dec 10 '14 at 10:45