Adding radio buttons logic in rails 4 - ruby-on-rails

I have a boolean field for gender in my data model. This is the script that I'm using to render the radio button on the view
<label class= 'sniglet'> Gender</label>
<%= f.radio_button(:gender, 'Male', :class => 'form-control radio-inline') %> Male
<%= f.radio_button(:gender, 'Female', :class => 'form-control radio-inline') %>Female
How do I write the logic that when the gender field is true the user is a female and false it's male.

Re-write your tags so they look like this:
<%= f.radio_button :gender, true, :class => 'form-control radio-inline' %> Male
<%= f.radio_button :gender, false, :class => 'form-control radio-inline' %>Female

we can use simple_form
<%= f.input :gender, :collection => ["Male","Female"], :as => :radio_buttons, :checked =>"Male",:item_wrapper_class => 'inline' ,:label => "Gender" %>

According to the Rails form helper documentation, you'll need to do something like this:
<%= f.radio_button :gender, true %>Female
<%= f.radio_button :gender, false %>Male
If you have a :gender attribute populated with either true or false, this should populate the radio buttons correctly for you. If it still doesn't work, you'll be best showing your form & controller code :)

Related

Submitting multiple attributes to controller from collection select in form

I want a collection select tag in rails to submit two attributes of the chosen item to the controller when the form is submitted. Basically, I have a list of counties and I want to submit both the county and the state as parameters. No problem having it submit one or the other, but not both. Am I thinking about this the wrong way? Here's what I have so far...
<%= form_tag(plans_path, method: 'get', action: 'screen2') do %>
<%= text_field_tag :ZIP, "ZIP Code", id: "zipBlur"%>
<%= collection_select(nil, :county, #counties.order('RES_RATIO DESC'), :COUNTY, :COUNTY_NAME, {:selected => "#{params[:county]}"}) %>
<%= submit_tag 'Screen', :name=> nil %>
<% end %>
Thanks for your help!
using :multiple => true
ex:
<%= collection_select(:ingredient, :supplier_ids,
Supplier.all(:order=>"name ASC"),
:id, :name, {:selected => #ingredient.supplier_ids, :include_blank => true}, {:multiple => true}) %>

progression bar in simple_form using ruby on rails

I am working on a project using ruby 2 and rails 4. I want to create a progression bar in simple_form. I got some resources from https://github.com/aarondo/progression.js/blob/master/demo/index.html
and it does work perfectly. But it was created using html, and i want to create using simple_form_for. My codes are:
<%= simple_form_for #kyc do |f| %>
<h2>Personal Information</h2>
<%= f.input :first_name , placeholder: 'Type Your First Name', label:'First Name', required: true, input_html: { :value => ''}, autofocus: true %>
<%= f.input :last_name , placeholder: 'Type Your Last Name', label:'Last Name', required: true, input_html: { :value => ''} %>
<%= f.input :pan , placeholder: 'Type Your PAN No', label:'PAN', required: true, input_html: { :value => ''} %>
<p align="center"><%= f.submit "Save", id: "kycs_button" %></p>
<% end %>
Can any one please help me to solve this problem? Thank you.
The only thing I can spot right away is that you did not put the data-progression attribute in and you didn't put the data-helper
If you ommit the data-progression attribute, the progress-bar always stays at 0%

simple_form label to be column value

I am using simple_form in rails and have a situation where I want the label to be the value from another field. In this case that field is not to be changed and so I don't want to be on the form.
To explain a bit better I have two lines that look like
<%= f.input :name, :label => false, :disabled => true, :input_html => { :class => 'input-small' } %>
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => false %>
What I'd like to do is have the first element to be the label of the second element. Now I could do this by having them inline, but I'd like them to be lined up with the other elements so that the labels and inputs are lined up.
so doing something like
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => f.name %>
or
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => {f.input :name, :label => false, :disabled => true} %>
Any thoughts on how to get around this?
Michael
'f.object' gets the object associated to that form and then you can get to the fields:
<%= f.select(:status, [["Not started","Not started"], ["Passed", "Passed"], ["Failed", "Failed"]]), :label => f.object.name %>
In the end I did indeed go for the inline option, which isn't ideal but did the job. However I had to do the following.
The main form set as a norm form
The block below that set as form-horizontal
then the subform partial defined as a none simple_form
<div class="control-group form-inline">
<div class="controls">
<%= f.text_field :name, :disabled => 'true', :size => 10 %>
<%= f.select(:status, [["Not started","Not started"], ["Passed", "Passed"], ["Failed", "Failed"]]) %>
</div>
</div>
If it was set as a simpleform the inline would not work as required. Again not perfect, and certainly not elegant, but worked

Passing URL Parameter to Drop Down

I have a simple form:
<%= f.input :type, :required => true, :collection => ["Nonprofit","School","Company"], :hint => "Note: nonprofits will need to provide proof of nonprofit status", :input_html => { :value => params['type'] } %>
<%= f.input :name, :label => "Organization" %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
A user gets to this page through a url like http://www.website.com/org/signup?type=Company
I can use this format to enter a value into a field like name or email, but can't figure out how to pass the param to the drop down.
I've already tried a few things including changing :value to :selected or :option but nothing seems to work.
Alright, figured it out! Posting here for future use.
<%= f.input :type, :required => true, :collection => ["Nonprofit","School","Company"], :hint => "Note: nonprofits will need to provide proof of nonprofit status", :selected => params['type'] %>
The trick is to drop the :input_html part and just use
:selected = > params['type']
Hope that helps someone in the future!

Grouping labels with simple_form

Is it possible to group inputs with simple_form?
I want "Postcode and city" to appear with one label and two inputs in one div as a group and not separately. But I cannot find a switch that turns off the label…
You can turn off the label on one input with the :label => false option.
Like this:
<%= f.input :name, :label => false %>
#rafaelfranca is right.
I am using Simple Form with an address like this:
<%= f.input :address, :placeholder => "Street + housenumber" %>
<%= f.input :zip, :label => false, :placeholder => "ZIP-code" %>
<%= f.input :locality, :label => false, :placeholder => "City/Town/Locality" %>
<%= f.input :state, :label => false, :placeholder => "State/Area" %>
<%= f.input :country, :label => false, :placeholder => "Country" %>
Then you will get only one label "Address" for the first text field. And all the next fields will show a placeholder.

Resources