0

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'
tbrisker
  • 15,518
  • 1
  • 16
  • 17
focode
  • 648
  • 1
  • 16
  • 39

2 Answers2

1

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.

tbrisker
  • 15,518
  • 1
  • 16
  • 17
  • some how the comments given on this thread helped me http://stackoverflow.com/questions/17982904/unpermitted-parameters-in-rails-4 – focode Jun 02 '15 at 18:53
  • 1
    In that case, you are missing the => part that makes it into a hash. `:delivery_days[]` is an attempt to access :delivery_days as an array without giving an offset, while `:delivery_days => []` defines it as a hash with an empty array. – tbrisker Jun 02 '15 at 20:12
0

Replace this

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

with

params.require(:profile).permit(:delivery_days)
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
thedanotto
  • 6,895
  • 5
  • 45
  • 43