I want to access three keys from params.
Say my params is:
params = {
'product_id' => 11,
'category' => {
'name' => 'Pet',
'sub_categories' => {5 => 'Name1', 7 => 'Name2'} ## **UPDATE**
'id' => 100
},
'user_action' => 'save'
}
Now I want to use strong parameters to filter these key out. i.e
#Inside controller
#action
def save_product_category
product_params[:product_id]
product_params[:category]
product_params[:user_action]
end
private
def product_params
params.permit(:product_id, :user_action, :category) # Doesn't work, eliminates 'category' key
end
How do I filter out these three keys?
UPDATE: I found one way to do it:
params.slice(:product_id, :category, :user_action)
Is it right way to do it?
UPDATE 2: Here is the correct answer:
params.permit(:product_id, :user_action, :category => [:id, :name, :sub_categories => {}])
Thanks @R_O_R and @twonegatives helping me out and bearing with me :)
Well you want
Nested Parameters