Rails : simple_form : Getting an empty string from checkbox collection - ruby-on-rails

I have the following code in my views
<%= f.input :role_names, as: :check_boxes, collection: #program.role_names %>
And whenever I submit the form I am getting values something like ["admin, "moderator", ""] but I was expecting something like ["admin, "moderator"] , why is this?
Moreover I made a inspect element, and there was a <input name="user[role_names][]" type="hidden" value=""> tag after the last check box, within the same control-group. I suppose this is getting added at the last in the params[:user][:recipient_role_names].
How should I handle this? I know I can do a reject(&:blank?) but is there a cleaner way on params[:user][:recipient_role_names]?
I also want to know why the input element is getting added? Is it a bug in simple form or I have done something wrong?
Other info:
simple_form gem version: 2.0.4
rails version: 3.2.8

It's a Rails' feature. You'll be able disable it in Rails 4. You can read more about this on simple form issue #603 and Rails issue #5402

Just add include_hidden: false to your input params:
<%= f.input :role_names,
as: :check_boxes,
collection: #program.role_names,
include_hidden: false
%>
And the empty string value won't be sent in the array.
Obs: I've just added the simple_form input code here for quick reference, as it was Vasily's answer that pointed me in the right direction. Thank you

Related

how to ensure rails multiselect input field passes all values correctly?

so, I've got a form inside a bootstrap modal.
when the modal loads, some javascript calls to the server to figure out what the current user has already chosen (this is an edit modal). once the server response returns, the form select sets itself to the values that the user has already chosen as is demonstrated in the below screenshot.
My problem occurs when the form is submitted. The params being sent to the server have the key 'room_object_id' that points to a single value. Naming issues of that key aside, I need that key to point to an array of values or a string or any structure that will hold multiple values and this is not the case, as demonstrated by the below screenshot.
My form is being generated by
<%= form_for :student, url: student_path, html: {class: 'form-horizontal sync'}, method: :put do |f| %>
<%= f.select(:room_object_id, options_for_select(#rooms.map {|room| [ room['name'], room['objectId'] ]}),{multiple: 'true', include_blank: ''} , {:class => 'select2-init form-control force-full-width', required: 'true', multiple: 'true', name: 'room_object_id'}) %>
<% end %>
I'm a bit unclear how select2 and the html form are working together and I have a feeling my problems are related to this. I'm also a bit unclear of the different usages of rails select, collection_select and options_for_select. I've looked at other SO posts on this topic but nothing pointed me in the right direction. Any pointers would be much appreciated.
Are you using the JavaScript required to run select2?
$(document).ready(function() {
$("#id_of_your_select_field").select2();
});
If you are, is your JavaScript console displaying any errors?

Simple Form - Radio Buttons

Please can someone help me with using Simple Form in my Rails 4 app.
I have a model with an attribute for a 'project_image'. I ask whether a bespoke image should be included and want a yes or no answer.
<%= f.collection_radio_buttons :project_image, label: 'Would you like add an image to your project pitch?', :options => [[true, 'Yes'], [false, 'No']], :first, :last, :style =>"display:inline", :default => true %>
I keep getting errors with this line of code. They best information coming out of my code editor is that there is a syntax error somewhere (syntax error, unexpected ',', expecting =>). I've tried removing all the commas, one by one and replacing with => and am not getting anywhere.
I tried following this answer and didn't find any success.
Simple form radio button
Help would be very much appreciated. Thank you

simple_form clickable value using link_to

hopefully a simple question.
Is it possible to make the value of a simple_form field a link_to?
I have tried
= f.input link_to(:project_id,project_path(#project_action_plan.project_id)), readonly: true
and also
= f.input :project_id, :input_html => {:value => link_to #project_action_plan.project_id, project_path(#project_action_plan.project_id)}, readonly: true
But am not getting the desired result.
Google only really brings up this stackoverflow result which was more about then using ajax etc and the simple_form Github doesn't mention anything. I am thinking it may not be possible and that I will have to make the label clickable.
The way the helper methods work is very specific. If you want to write your own custom helper then it's possible to create your own arbitrary formatter methods by creating a subclass of FormBuilder and using that when rendering your forms.
More information on this subject is available in the documentation on Form Helpers.

formtastic check_boxes using collection not saving checked items in edit form

I know this was supposed to be fixed in Formtastic 2.0 but for some reason, this isn't working for me.
<%= f.input :gender, :as => :check_boxes, :collection => ["Male", "Female", "Unisex"] %>
Whenever I go back and check it, the items is always unchecked again. It never appears as selected. I know Justin French commented about it here, but I am not sure what it means or how that can help me fix the problem.
I am saving gender as a string in the User model if it makes a difference.
I am also using it with formtastic-bootstrap 1.1.1
Thank you!

Rails drop down box posting null

I have the following drop down box in a form for rails:
<%= f.select (:boolean, options_for_select([["Yes", 1], ["No", 0]])) %>
Other drop down boxes in the same form post correctly, but this one posts null. Others in the same form:
<%= f.select (:kids_in_college, %w{1 2 3 4 5 6 7 8}) %> #posts correctly
<%= f.select (:year, %w{2009-2010 2010-2011 2011-2012}) %> # posts correctly
Is there something wrong with my syntax?
I think problem is not with the select list.
I think you have to handle it on controller side.
Something like following
#obj.boolean = (params[:obj][:boolean]=="1")? true : false
Note:- As per my knowledge most likely problem will be you are trying to input "String" in the field where boolean is expected.

Resources