Question

In a web request, my initial params is like this, without including the unrelated part:

params = {
  "base_table"=>{"name"=>"syy", "age"=>"12"},
  "tennis_questionaire"=>{"tennis_ball_num"=>"3718"},
  "id"=>"1"
}

I want to put the 'base_table' under "tennis_questionaire", because it belongs_to tennis_questionaire, and I want to update the two objects using just one update method. I.e., the end result I want is something like this:

params[:tennis_questionaire] =
  {"tennis_ball_num"=>"3718", "base_table"=>{"name"=>"syy", "age"=>"12"}},

In order to achieve this, I created an action_filter to pre-process the params, like this:

before_action :update_param, only: [:create,:update]

def update_param
  params[:tennis_questionaire][:base_table] = ActionController::Parameters.new(
    params.require(:base_table).permit(:name, :age)
  ) 
end

However, when I was trying to permit it in the actual code of processing the params (the last step before I pass it to update method):

params.require(:tennis_questionaire).permit(:tennis_ball_num,:base_table)

I found that the :base_table is not permitted.

Why is that so? Any suggestions on how to make it work~?

Thanks a lot

Was it helpful?

Solution

I found why after some research. Referring to: docs on permit, if one wants to permit an array/object, one need to specify the elements that needs to be permitted. Additionally, there's no way to permit the whole array/object.

So for my case above, I should put:

params.require(:tennis_questionaire).permit(:tennis_ball_num,base_table:[:name, :age]) instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top