Abhi Abhi - 23 days ago 6
Ruby Question

Permit custom created params in rails

I have already gone through following posts, without getting any satisfactory answers:

can't permit custom params with strong parameters

Permit extra params in special cases with Strong Params in Rails 4

What I want is to permit my custom created params in rails controller:

MY CODE

Template

= form_tag ...
= select_tag :hour, options_for_select(options_for_hours), name: "clinic_hour[close_time][]", title: "Hours"
= select_tag :minute, options_for_select(options_for_minutes), name: "clinic_hour[close_time][]", title: "Minutes"
= select_tag :convention, options_for_select([["AM", "AM"], ["PM", "PM"]]), name: "clinic_hour[close_time][]"
= submit_tag ...


The above code creates params like:

Parameters: {"clinic_hour"=>{"close_time"=>["0", "0", "AM"]}}


But in controller...

Controller

When I do like:

def clinic_hour_params
params.require(:clinic_hour).permit(
:close_time
)
end


It still says this, in rails server log:

Unpermitted parameters: close_time
{}


What's wrong?

Answer

You have an array here, which means, you need to specifically tell to accept an array. Try this:

def clinic_hour_params
  params.require(:clinic_hour).permit(
    :close_time => []
  )
end

Also here https://github.com/rails/strong_parameters you can see which all types are "permittable".

Comments