How to use integer radio buttons - ruby-on-rails

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' %>

Related

How to pass an array of string through the rails' form

I have a trouble with the rails' form, I need to pass to the form of Event an array of emails, I have this attribute members that I want to have like array. I try in this way but it doesn't work, can you help me?
<div class="field">
<%= form.fields_for :members do |p| %>
<% #group.list_accepted.each do |part| %>
<%= p.label :email , User.find(part.user_id).username %>
<%= p.check_box :email, {} ,User.find(part.user_id).email, nil %>
<br>
<%end%>
<% end %>
</div>
params.require(:event).permit(:title, :description,:user_id, :group_id, :start_date, :end_date, members: [:email])
the result of this is a set with just the first email {email=> "email#.com"}

rails how to pass nested parameters

I have a strong parameter like this
params.require(:survey).permit( option_booleans_attributes: [:id, :survey_id, :topic, :answer])
if I use rails f.input I got parameter like
"option_booleans_attributes"=>{"0"=>{"answer"=>"true", "id"=>"5"}, "1"=>{"answer"=>"false", "id"=>"6"}, "2"=>{"answer"=>"true", "id"=>"7"}}}
but I need to show option_booleans topic and let user fill answer
<% #survey.option_booleans.each do |question| %>
<%= question.topic %><br>
<%= radio_button "option_booleans_attributes[]", "answer[#{question.id}]", "true" %>是
<%= radio_button "option_booleans_attributes[]", "answer[#{question.id}]", "false" %>否<br>
<% end %>
But I don't know how to generate the 0 1 2 in the parameter..
about my survey.rb
class Survey < ActiveRecord::Base
has_many :option_booleans
accepts_nested_attributes_for :option_booleans
belongs_to :member
end
class OptionBoolean < ActiveRecord::Base
belongs_to :survey03
and OptionBoolean have topic:string and answer:boolean
I want to let user see the topic and update the answer
It seems you're missing several parts of your form:
#app/controllers/surveys_controller.rb
def new
#survey = Survey.new
#survey.option_booleans.build #-> creates ActiveRecord Object for your form
end
#app/views/surveys/new.html.erb
<%= form_for #survey do |f| %>
<%= f.fields_for :option_booleans do |option| %>
<% #survey.question_booleans.each do |question| %>
<%= question.topic %>
<%= f.radio_button "answer[#{question.id}]", "true" %>
<%= f.radio_button "answer[#{question.id}]", "false" %>
<% end %>
<% end %>
<% end %>
Although I don't quite understand how your system works
You have a survey with many options, but you're passing new attributes to create new options each time. Surely you'd have option_booleans as a join model, with options as an independent model, and:
#app/models/survery.rb
Class Survey < ActiveRecord::Base
has_many :option_booleans
has_many :options, through: :option_booleans
end
What I use now
<li><% #survey03.option_booleans.each do |question| %>
<%= question.topic %><br>
<%= radio_button "option_booleans_attributes[#{question.id}]", "answer", "true", :required => true %>
<%= radio_button "option_booleans_attributes[#{question.id}]", "answer", "false", :required => true %>
<% end %></li>
and in controller
params[:option_booleans_attributes].each do |option_id, attributes|
if option = OptionBoolean.find_by_id(option_id)
option.update_attributes(attributes)
end
end
and I know if I want to generate index just use
<% #survey.option_booleans.each_with_index do |question, index| %>
<%= question.topic %><br>
<%= radio_button "option_booleans_attributes[#{index}]", "answer[#{question.id}]", "true" %>
<%= radio_button "option_booleans_attributes[#{index}]", "answer[#{question.id}]", "false" %>
<% end %>
try select options like this:
#variable_with_booleans = [true, false]
<%= f.select :param, #variable_with_booleans %>
answer to your latest comment:
I don't sure, but try to edit your form
<%= form_for #question do |f| %>
<%= f.label :topic %>
<% #variable_with_booleans = [true, false] %>
<%= f.select :answer, #variable_with_booleans %>
<%= f.submit %>
<% end %>
and edit your strong params in controller,which contains this action

Rails 3 serialized form attribute

I have the following model:
class Deck < ActiveRecord::Base
has_many :cards
serialize :stats
attr_accessible :name, :description, :stats
end
In this, stats should be an array of strings. I want to be able to add an arbitrary number of stats to the row (using javascript to add additional "stat" input boxes, but that is outside the scope if the question.)
My question is how do I reflect this structure in the view? If I was building the form manually then it would look like:
<input type="text" name="deck[stats][]">
My view currently looks like:
<%= f.fields_for :stats, #deck.stats do |p| %>
<%= p.label :p, "Stats: " %>
<%= p.text_field :p %>
<% end %>
But this outputs:
<input type="text" name="deck[stats][p]">
I also want a counter so I can display the label as "Stat 1:", "Stat 2:", etc.
Any help is appreciated.
Posting the solution I went for to aid future readers.
#Controller
def new
#deck = Deck.new
#deck.stats = ["", ""] #include for number of times you wish to show the field.
end
#View
<%#deck.stats.each_with_index do |s,i| %>
<p>
Stat <%=i+1%>: <%=text_field "stat", i, :name => "deck[stats][]", :value => s %>
</p>
<%end%>
By defining :p you are making it think you are looking for a method on stats called p, ie stats.p
<%= f.fields_for :stats, #deck.stats do |p| %>
<%= p.label :p, "Stats: " %>
<%= p.text_field :p %>
<% end %>
Rather just try <%= p.text_field %> drop the :p
I don't know if there's a clean way of doing it, but this should work:
<% #deck.stats.each do |value|
<%= f.fields_for :stats do |fields| %>
<%= fields.text_field "", :value => value %>
<% end %>
<% end %>
= simple_form_for [:stats, #deck] do |f|
- 1.upto(4) do |i|
= f.input_field :stats, multiple: true, label: "Stat #{i}"
= f.submit

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

Rails checkboxes

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 %>

Resources