1
votes

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?

2

2 Answers

1
votes

If you look at the README for Strong Parameters under Permitted Scalar Values you will see that since you're working with an array of scalar values you'll have to denote it like this:

def clinic_hour_params
  params.require(:clinic_hour).permit(
    close_time: []
  )
end
1
votes

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".