Rails 4 params.require().permit() not considering

2019-07-24 09:37发布

问题:

I am having trouble using the params.require().permit() method because it is not considering one of the attributes I'm setting to it, namely the attribute :on

What I have is this:

def event_recurrence_params
  params.require(:event_recurrence).permit(:room_id, [...], :on, :every)
end

Then, when I call event_recurrence_params, the answer is

>> event_recurrence_params
=> {"room_id"=>"1", [...], "every"=>"week"}

even though the value of params[:event_recurrence] is

>> params[:event_recurrence]
=> {"room_id"=>"1", [...], "on"=>["wednesday"], "every"=>"week"}

As you can see, the :on attribute is not being considered in the event_recurrence_params even though the attribute is permitted and the params[:event_recurrence] include it.

Any ideas? I've tried lots of things but none of it worked. Thank you!

回答1:

From the documentation:

The permitted scalar types are String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO, ActionDispatch::Http::UploadedFile and Rack::Test::UploadedFile.

To declare that the value in params must be an array of permitted scalar values map the key to an empty array:

So you need to do this:

params.require(:event_recurrence).permit(:room_id, [...], :every, on: [])