Rails checkboxes - ruby-on-rails

I have a string column in my database which is either "artist" or "listener", and I want the user to be able to choose which string the column is populated with by clicking the appropriate checkbox. How would I do this?

You should use radio button here:
# Imagine it is User model and user_type field
<%= form_for User.new do |f| %>
<%= f.radio_button :user_type, "artist" %>
<%= f.radio_button :user_type, "listener" %>
<% end %>

f.check_box :my_field, {}, "artist", "listener"
This would make my_field be "artist" when it's checked, and "listener" when unchecked.

You should use radio-buttons for that matter. Also make sure to put that logic into the model (validations).
# model
class User
TYPES = %w(artist listener)
validates_inclusion_of :user_type, :in => TYPES
end
# view
<%= form_for :user do |f| %>
<% User::TYPES.each do |type| %>
<%= f.radio_button :user_type, type %>
<% end %>
<% end %>

Related

Radio buttons in rails not taking default value based on the data stored in the database

Am trying to create a profile page where the user can check "male" or "female". After saving the form, whenever the user visits the page again, the gender must be set by default based on the data stored in the database.
Am using the form_for helper here.
<%= form_for #profile do |f| %>
<%= f.label :gender, "Male", :value => "m" do %>
<%= f.radio_button :gender, "m" %>
<% end %>
<%= f.label :gender, "Female", :value => "f" do %>
<%= f.radio_button :gender, "f" %>
<% end %>
<% end %>
I this you should use like this way:
<% form_for( #profile) do |f| %>
<%= f.radio_button :gender, "Male", :checked => #profile.is_male? %>
<%= f.radio_button :gender, "Female",:checked => #profile.is_female? %>
<% end %>
In your models/profile.rb:
class Profile
def is_male?
#write your code for check male
end
def is_female?
#write your code for check female
end
end

Rails: How to customise the order of fields in a form with nested fields

I have a form that saves attributes to two models Company and nested model Address in a single form using simple_nested_form_for and simple_fields_for.
The attributes:
Company:
-legal form
-uploads
Address:
-name
-website
In my form I wish to change the order of the fields
-name (Address model)
-legal form (Company model)
-website (Address model)
-uploads (Company model)
thus interchanging the form attributes and nested_field attributes
I tried the following, but that did not seem to work.
<%= simple_nested_form_for #company do |f| %>
<%= f.simple_fields_for :address do |c| %>
<%= c.input :name %>
<% end %>
<%= f.input :legal_form %>
<%= f.simple_fields_for :address do |c| %>
<%= c.input :website %>
<% end %>
<%= f.input :uploads %>
<% end %>
How do I make this work?
While it doesn't look like all your attributes line up ( you said uploads was address and website was in company in your attributes list but the form doesn't match that ) there is still a simple solution to your question.
Nested fields rely on their builder to denote which part of the params hash to put them in. So simply call the builder that you need.
<%= simple_nested_form_for #company do |f| %>
<%= f.simple_fields_for :address do |c| %>
<%= c.input :name %>
<%= f.input :legal_form %>
<%= c.input :website %>
<%= f.input :uploads %>
<% end %>
<%= f.submit %>
<% end %>
In each place that you need the company fields, you'll call builder f and in each place you need the address fields, you'll call builder c - but nested them all inside the lowest common denominator so they're all available.

How to use integer radio buttons

What is the simplest way to represent an integer attribute with a limited number of valid values as radio buttons?
How can I use the formhelper to achieve this? I only see examples that use string values.
You can try
<%= form_for :model do |f| %>
<% 1.upto(10) do |i| %>
<%= f.radio_button :integer, i %> #integer is the model field
<% end %>
<% end %>
or
<%= form_tag do %>
<% 1.upto(10) do |i| %>
<%= radio_button_tag :name, i %>
<% end %>
<% end %>
Something like:
<% (1..10).each do |value| %>
<%= f.radio_button_tag(:method_name, value) %>
<% end %>
Where (1..10) might belong into the model as a constants. What also makes validations easier:
# in the model
FOOS = (1..10)
validates :foo, inclusion: { in: FOOS }
# in the view
<% Model::FOOS.each do |values| %>
<%= f.radio_button_tag(:foo, value) %>
<% end %>
My way is to use an enum in your model. E.g the role field will be saved as number 0 if the role is investor, number 1 if the role is trader in my example.
You need to declare your enum in your model .rb file like this:
enum role: [:investor,:trader]
Then in the view file:
<%= f.radio_button :role, :investor , class: 'form-control underlined' %>
<%= label :role_investor, 'I wanna be an investor' %>
<%= f.radio_button :role, :trader , class: 'form-control underlined' %>
<%= label :role_trader, 'I wanna be a trader' %>

Radio Button Helper - Default value

I have a table rooms and I have a field type(Single, Double). I want to use a radio button for this field. So, I am using it like this:
<% form_for(#room) do |f| %>
<%= f.radio_button :type, "Single" %>
<%= f.radio_button :type, "Double" %>
<% end %>
This works fine for edit view. The problem is that for the new view, I want to default the radio button to "Single". For this code, no value is checked for the new view.
I am now adjusting that with condition check
<% form_for(#room) do |f| %>
<%= f.radio_button :type, "Single", :checked => #room.new_or_single? %>
<%= f.radio_button :type, "Double" %>
<% end %>
Room model
def new_or_single?
type.nil? or type == "Single"
end
Is there a better way to achieve this?
Set default :type in constructor
def new
#room = Room.new(:type => "Single")
end

Ruby on Rails: Drop down menu

I'm trying to create a drop down menu to allow a user to change an entry's field in my table. The user has one of three options -- hot, medium and cold.
I already have text_fields that do essentially the same thing for other fields, that all update when the user clicks on a submit_tag.
Is there an easy way to implement a drop-down box and have the result saved with the submit_tag ?
thanks,
-Chris
Here's the basic answer. The array of two element arrays is the critical part.
<% form_for #entry do |f| %>
<%= f.text_field :name %>
<%= f.select :temperature, [['Hot','hot'],['Medium','medium'],['Cold','cold']] %>
<%= f.submit %>
<% end %>
I'll assume 2 things:
That you are the <%= form_for #model_instance idiom (explained on section 2.2 of this guide).
That you want to store the "hot", "medium" and "cold" values as strings (not as numbers 1,2 and 3 or something similar) on your database.
Let's say that you have two fields, called :name and :temperature, controlled by two text_fields:
<% form_for #article do |f| %>
<%= f.text_field :name %>
<%= f.text_field :temperature %>
<%= f.submit "Create" %> <% end %>
<% end %>
Now you want to change the :temperature control to a dropdown list, accepting hot, medium and cold as values. Then you can do that this way:
<% form_for #article do |f| %>
<%= f.text_field :name %>
<%= f.collection_select :temperature, Article::TEMPERATURES, :to_s, :to_s,
:include_blank => true
%>
<%= f.submit "Create" %> <% end %>
<% end %>
You will now have to define the Article::TEMPERATURES constant in your Article model. It shouldn't be very difficult:
class Article < Activerecord::Base
TEMPERATURES = ['hot', 'medium', 'cold']
You may be wondering why I added the :include_blank part on the collection_select. This will add an "empty" option on your dropdown list. You will need that empty option when creating new objects, unless you want a "default" value to temperature.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001730
I was working on something similar. I got this to work by simply adding either an enum or a constant(similar to what kikito said previously) in my model and then calling the select in my form.
Here's how it can work.
using the constant:
class ClassName < ActiveRecord::Base
TEMPERATURES = ['Hot', 'Medium', 'Cold']
end
bin/rails g migration add_column_to_table temperatures:string
_form.html.erb
<%= f.label :temperature %>
<%= f.select :temperature, ClassName::TEMPERATURE %>
or
using the enum:
class ClassName < ActiveRecord::Base
enum temperature: [:hot, :medium, :cold]
end
bin/rails g migration add_column_to_table temperatures:integer
_form.html.erb
<%= f.label :temperature %>
<%= f.select :temperature, ClassName.temperatures.keys %>
Hope that helps you!
You might want to consider formtastic gem which is lot less code.
<% semantic_form_for #stuff do |f| %>
<% f.inputs do %>
<%= f.input :name %>
<%= f.input :temperature, :as => :select,
:label => "Degree", :include_blank => false,
:collection => [["Hot", 1], ["Medium", 2], ["Cold", 3]] %>
<% end %>
<%= f.buttons %>
<% end %>
In accordance with all of the above answers, remember to do this last, important step:
Restart your server!
As a newbie, I was wondering why my array was not working even though I followed all the steps correctly.

Resources