Skip to content

collection_radio_buttons doesn't work in Rails 5 #22231

@kyamaguchi

Description

@kyamaguchi

When I use collection_radio_buttons on the form and submit it, I get an error and a blank page on the browser.
(Rails version: commit 1f8cc53 , Date: Sun Nov 8 17:40:46 2015 -0800)

The issue #18303 might be related.

In app/views/posts/_form.html.erb, I have

  <div class="field">
    <%= f.label :choice %><br>
    <%= f.collection_radio_buttons :choice, {choice1: 'Choice 1', choice2: 'Choice 2'}.map{|k,v| OpenStruct.new(value: v, label: k) }, :value, :label %>
  </div>

HTML

  <div class="field">
    <label for="post_choice">Choice</label><br>
    <input type="radio" value="Choice 1" name="post[choice]" id="post_choice_choice_1" /><label for="post_choice_choice_1">choice1</label><input type="radio" value="Choice 2" name="post[choice]" id="post_choice_choice_2" /><label for="post_choice_choice_2">choice2</label><input type="hidden" name="post[choice][]" value="" />
  </div>

When I submit the form, I see the following error in log.

Started POST "/posts" for ::1 at 2015-11-09 15:01:12 +0900

ActionController::BadRequest (Invalid request parameters: expected Array (got String) for param `choice'):
  /gems/rack-c393176b0edf/lib/rack/query_parser.rb:93:in `normalize_params'
  /gems/rack-c393176b0edf/lib/rack/query_parser.rb:107:in `normalize_params'
  /gems/rack-c393176b0edf/lib/rack/query_parser.rb:67:in `block in parse_nested_query'
  /gems/rack-c393176b0edf/lib/rack/query_parser.rb:64:in `each'
  /gems/rack-c393176b0edf/lib/rack/query_parser.rb:64:in `parse_nested_query'
  /gems/rack-c393176b0edf/lib/rack/request.rb:458:in `parse_query'
  /gems/rack-c393176b0edf/lib/rack/request.rb:360:in `POST'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_dispatch/http/request.rb:355:in `block (2 levels) in POST'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_dispatch/http/parameters.rb:54:in `block in parse_formatted_parameters'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_dispatch/http/parameters.rb:54:in `fetch'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_dispatch/http/parameters.rb:54:in `parse_formatted_parameters'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_dispatch/http/request.rb:354:in `block in POST'
  /gems/rack-c393176b0edf/lib/rack/request.rb:74:in `fetch'
  /gems/rack-c393176b0edf/lib/rack/request.rb:74:in `fetch_header'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_dispatch/http/request.rb:353:in `POST'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_dispatch/http/parameters.rb:26:in `parameters'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_dispatch/http/filter_parameters.rb:35:in `filtered_parameters'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_controller/metal/instrumentation.rb:21:in `process_action'
  /gems/rails-1f8cc53689ea/actionpack/lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
  /gems/rails-1f8cc53689ea/activerecord/lib/active_record/railties/controller_runtime.rb:18:in `process_action'

In Gemfile, I have

gem 'rails', '5.0.0.alpha', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', '2.0.0.alpha', github: 'rack/rack'

In Gemfile.lock, I have

GIT
  remote: git://github.com/rack/rack.git
  revision: c393176b0edf3e5d06cabbb6eb9d9c7a07b2afa7
  specs:
    rack (2.0.0.alpha)
      json

GIT
  remote: git://github.com/rails/arel.git
  revision: 3c429c5d86e9e2201c2a35d934ca6a8911c18e69
  specs:
    arel (7.0.0.alpha)

GIT
  remote: git://github.com/rails/rails.git
  revision: 1f8cc53689ead6befbbb6d8d7436596270738ff3


I can work around with include_hidden: false option and it is OK in most cases.

  <div class="field">
    <%= f.label :choice %><br>
    <%= f.collection_radio_buttons :choice, {choice1: 'Choice 1', choice2: 'Choice 2'}.map{|k,v| OpenStruct.new(value: v, label: k) }, :value, :label, include_hidden: false %>
  </div>

And It is also OK with rails 4.2.4 and rack 1.6.4.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions