Rails and forms: drop down with range of numbers and Unlimited - ruby-on-rails

I have this right now:
<%= f.select :credit, (0..500) %>
This will result in this:
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
...
How would I add another option in that select that would be "All" and which value should be nil?

This will almost do what you want:
<%= f.select :credit, ((0..500).map {|i| [i,i] } << ["No limit",nil]) %>
select can take a number of formats for the list of options. One of them is an array of arrays, as given here. Each element in the outer array is a 2-element array, containing the displayed option text and the form value, in that order.
The map above turns (0..500) into an array like this, where the option displayed is identical to the form value. Then one last option is added.
Note that this will produce a value of "" (an empty string) for the parameter if "Unlimited" is selected - if you put a select field into a form and the form is submitted, the browser will send something for that form parameter, and there is no way to send nil as a form parameter explicitly. If you really wanted to you could use some clever javascript to get the browser to not send the parmeter at all, but that would be more work than simply adding:
param[:credit] == "" and param[:credit] = nil
to your controller action.

If I understand the question correctly, you can use options_for_select and prompt to do this a little more cleanly than what is shown in the selected answer:
<%= f.select :credit, options_for_select(0..500), { prompt: "No Limit" } %>
See the docs here:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select

Related

How to show more attributes in each element of the collection with collection_select

I have this with simple form:
= simple_form_for(Note.new, remote: true) do |f|
= f.collection_select :dance_id, current_user.dances.includes(:style).all,
:id, :style_name, {prompt: "Please select dance"}, {class: "form-control"}
Code generated:
<select class="form-control" name="note[dance_id]" id="note_dance_id">
<option value="">Please select dance</option>
<option value="39">Adowa Dance (Ghana)</option>
<option value="38">Adowa Dance (Ghana)</option>
<option value="37">Tango (Argentina)</option>
</select>
which works fine as it is displaying all dances by style_name as i want it. But i want to make a difference between two dances and want to also show level and date right next to each dance in the collection because there are many dances with same name bc of the style which is the same!
please help. any better way to do it with simple form helpers?
As stated in the discussion, you needed to declare a method in your model's rb file like this:
def custom_name
"#{style.name}. #{date}"
end
And change the :style_name parameter to :custom_name

Passing multiple parameters with one radio_button_tag

I currently have:
radio_button_tag :item, value
However I want a radio_button_tag that I passes multiple parameters on submit.
Something like this:
radio_button_tag "item", "value", "item2", "value2"
Use a select not a radio button. The whole point of radio buttons is that only a single button per input (group) can be active at the same time.
Radio buttons on MDN
<label>Whats for breakfast?</label>
<select name="select" multiple="true">
<option value="value1">Eggs</option>
<option value="value2" selected>Bacon</option>
<option value="value3">Oatmeal</option>
</select>
Rails has several helpers for creating selects and option tags. http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease
Check out this answer: Rails radio_button_tag how to send multiple parameters
In essence, pass back and forth an array with the params you need, and use the .split[] method to retrieve them in the controller.

assigning value to "default selection parameter" in select_tag - ruby/rails

following is the code of my select tag
<%= select_tag "assignee#{cnt}", options_from_collection_for_select(#arr,tmp="id","name" , default_selection), html_options = { :onblur => "myblur(this);", :onChange=> "submit_selected(this);", :style=> "visibility:visible;" } %>
the html generated for above line is like:
<select id="assignee1" name="assignee1" onChange="submit_selected(this);" onblur="myblur(this);" style="visibility:visible;">
<option value="12">Name1</option>
<option value="48">Name2</option>
<option value="15">Name3</option>
<option value="35">Name4</option>
</select>
now in the default_selection paramter, i want to use the option value corresponding to option text. i have the option text.
say, i have "Name1" and i want to use its option value i.e 12 in default_selection parameter
any comments?
any comments?
Yes ;) Indeed your question is not related to this portion of code. You just have to search in your object #arr.
My bet :
default_opt = #arr.select {|o| o.name == 'Name1'}
default_selection = default_opt.id if default_opt
Then generate your <select> as you have already done.

rails - How to set value of a select list from a variable?

I want to set the value in a select list based on the value of a variable.
The variable here is #email_setting.frequency.
In the view the code looks like:
<%= select('email_setting', 'frequency','<option value="1">Daily</option>
<option value="2">Immediately</option>
<option value="3">Every Monday</option>
<option value="4">Every Tuesday</option>
<option value="5">Every Wednesday</option>
<option value="6">Every Thursday</option>
<option value="7">Every Friday</option>
<option value="8">Every Saturday</option>
<option value="9">Every Sunday</option>',
:class=>'fl-space2 required size-120',
:selected=>#email_setting.frequency) %>
I have tried few variations of the following with no luck.
Any advice on how to get this working right?
thanks
First check whether the value of #email_setting.frequency. Actually if you give 'email_setting', 'frequency' as the first 2 parameters, the selected value will be #email_setting.frequency. I believe it is actually integer(like 1) and you are providing string as the option value(like "1"). That should be the reason why it is not selected. Try
<%= select('email_setting', 'frequency', [['Daily', 1],['Immediately', 2], ..], {}, :class=>'fl-space2 required size-120' %>
Also the 4th parameter of select is options and the 5th parameter is html_options. So if you want to give html options like selected, class, you should provide it as 5th parameter by providing 4th parameter options as an empty hash. If you really want to give selected also, you should do that like this:
<%= select('email_setting', 'frequency', [['Daily', 1],['Immediately', 2], ..], {}, :class=>'fl-space2 required size-120', :selected => #email_setting.frequency %>
But the first would be enough in your case.
See select rails api

Selection dropdown in Ruby on Rails: selected value not being picked up

I've got this problem where I get the dropdown showing but the selected option is not showing up. Here's the code I have to generate the selection dropdown in the .erb:
<%= collection_select("url", "source_type_id", #source_types, :id, :name, {:prompt => "Please select..."}) %>
The #source_types is populated in the controller from a lookup table that is tied to the model. #url_object is the model:
#source_types = SourceType.all
Because of the way the model is tied to the lookup table:
belongs_to :source_type
#url_object.source_type_id returns the numeric value, and #url_object.source_type returns the associated name from the lookup table.
<select id="url_source_type_id" name="url[source_type_id]"><option value="">Please select...</option>
<option value="1">Dictionary/Thesaurus</option>
<option value="2">Encyclopedia</option>
<option value="3">Magazine</option>
<option value="4">Map/Atlas</option>
<option value="5">Newspaper</option>
<option value="6">Reference Tools</option></select>
I read the API for this method and the implication is that if the source_type_id is present the collection_select will automagically pick it up and set the selected value, but this is clearly not happening.
I'm hoping someone will see what obvious mistake I've made here...
Hope this will help
<%= f.select :source_type_id, #source_types.collect{|p| [p.name, p.id] }, params[:source_type_id] %>

Resources