问题描述:

I am using simple_form in rails, and I have this as checkbox:

<%= f.input :delivery_days, as: :check_boxes,

collection: [

'Sunday',

'Monday',

'Tuesday',

'Wednesday',

'Thursday',

'Friday',

'Satusday',

'All'],

wrapper: :vertical_radio_and_checkboxes

%>

I am not able to get the values of selected checkboxes in my controller.

I am using the following code to get it:

def user_basic_params

logger.info "from user_basic_params"

params.require(:profile).permit(:delivery_days[])

end

I have other form fields but they are getting handled correctly and I am able to get their values.

Console output is :

Started POST "/profiles" for 127.0.0.1 at 2015-06-02 02:07:56 +0530

Processing by ProfilesController#create as HTML

Parameters: {"utf8"=>"✓", "authenticity_token"=>"w3RvztJtKq56qimpZPj3KD9S5pr1vbw2K6b7eToaEV5rEuBsx1jqJ8gITg+uHq8TgBoeYZPsczQLghWg2fhpCg==", "profile"=>{"milk_animal"=>"Any", "milk_type"=>"Any", "brand"=>"Amul", "time_of_delivery(1i)"=>"2015", "time_of_delivery(2i)"=>"6", "time_of_delivery(3i)"=>"1", "time_of_delivery(4i)"=>"20", "time_of_delivery(5i)"=>"37", "start_date(1i)"=>"2015", "start_date(2i)"=>"6", "start_date(3i)"=>"1", "delivery_days"=>["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satusday", "All", ""], "name"=>"Sony", "address"=>"asdfghjk", "pincode"=>"zxcvbnm,", "contact_no"=>"2345678"}, "commit"=>"Create Profile"}

from create

from user_basic_params

Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)

ArgumentError (wrong number of arguments (0 for 1..2)):

app/controllers/profiles_controller.rb:53:in `[]'

app/controllers/profiles_controller.rb:53:in `user_basic_params'

app/controllers/profiles_controller.rb:18:in `create'

网友答案:

The problem is in the permit(:delivery_days[]) part of your code. Rails expects the brackets to contain 1 or 2 parameters, but you are passing none, thus raising an error. I am not sure why the brackets are there, try removing them and I believe your code should work.

网友答案:

Replace this

params.require(:profile).permit(:delivery_days[])

with

params.require(:profile).permit(:delivery_days)
相关阅读:
Top