How to display selected value from the drop down menu in Rails? - ruby-on-rails

I am a Rails newbie. I have created a drop down using collection_select and grouped_collection_select. My issue is that I am not able to display the selected option value from the drop down list. I have to display only the selected value on a new page but I am not able to get it.

Use the :selected => params[:name_of_form][:name_of_field]
= f.select :category,
options_for_select(['option', 'value']... etc ),
:selected => params[:user][:category]
I cant remember if the symbol goes on the inside like above, or if it
You should really be more clear when you are asking questions on SO, and you should also include code.
ALWAYS ALWAYS ALWAYS INCLUDE CODE!

Related

Can I disable select2 in just one simple_form of my code?

I need to insert a dropdown list in a simple_form collection. I added the line below and it did not work. When I click in the dropdown-box nothing happens and it should show me the collection of itmes (2 email addresses).
= f.input :recipient, :collection => %w[contato#wit.com suporte#wit.com]
When I inspected the code I noticed that everytime I cliked in the dropdown-box something was changing in the code.
From this:
To that:
Then I went to check what was select2 and started to suspect that it migth be the reason why the dropdown-list was not working. Are there any way I can disable Select2 at this part of the code so I can test if this is the reason why my code does not work ? Why is html showing two containers that apparently do the same thing and why the email adresses do not appear ? Find below html which shows select and span containers.

Rails Form: f.select for multiple options

For my user form, I have a hobbies drop-down menu and I want to be able to select more than one option (one user may have skiing, reading, and chess as hobbies).
Of course, doing this is very easy!
However, none of the available options seem to work for me...
Here's my code:
<%= f.select :hobbies, [['Chess','chess'],
['Movies','movies'],
['Videogames','videogames'],
['Skiing', 'skiing'],
['Reading','reading']],
{:multiple => true} %>
However, when I look at my form, I don't think this is working. It makes the drop-down menu....but how do I select multiple entries? I try ctr + click but it doesn't do anything....what am I missing? It keeps just selecting one value only...
Take a look at the accepted answer to that question - the method signature is:
select(:type, [data], {options hash}, {second options hash})
And in the answer, it has multiple: true in the second options hash.
API dock for select_tag gives a hint about what the two different hashes are for - it looks like the first options hash is for "option_tags", and the second one is for "options"

Rails Populating SELECT options from database

I feel like this is basic but can't find details anywhere.
I have a basic application created by generating scaffold.
There is form already built into this that enters data. I have a SELECT (drop down) box in the form. I would like to be able to have the OPTIONS in this select box be pulled from a database. Ideally, the end user would be able to add and edit these options.
I have no idea how to link that SELECT field in my form to where the OPTIONS will be STORED.
Can someone point me in the right direction as far as terminology as what to research? I'm coming up blank. I feel like this must be a common thing to do..
As ByScripts' link includes, here is the script that worked for me (for those where time is not a luxury):
<%= collection_select(:lang_id, 0, Language.all, :id, :name) %>
Where Language is a table with one column called 'name' and an auto-assigned column id; :lang_id is the name of the element and 0 is the default selected index when the page loads.

radio_button_tag in rails 3.1

I have a query to ask. Im using radio_button_tag with rails 3.1. The issue that I come accross is that for eg : If I have ascending and descending radio buttons, only one should be selected. But in my case both the radio button tags are getting selected.
Kindly help me.
This is a guess at best, because you haven't put your code, but:
You need to ensure that your radio buttons have the same HTML name attribute by giving your tags the same first parameter in radio_button_tag. See the documentation.
if two radio fields has same name, one can be selected at a time!
Pass true to the tag you want to be selected initially. For example, if you want 'Descending' to be checked initially, write like:
radio_button_tag 'order', 'asc'
radio_button_tag 'order', 'desc', true

Setting hint text in a text field in Ruby on Rails

Can some one suggest the best way for setting hint text(not default text) for a text field in Ruby on Rails. Currently I am using this:
<%= text_field_with_auto_complete
"customer",
:contact_person, {
:value => 'last, first',
:style => 'color:#aaa;width:11em;',
:onfocus => "if(this.getValue()=='last, first'){this.clear();this.style.color = '#000';}",
:onblur => "if(this.getValue()==''){this.setValue('last, first');this.style.color = #aaa';}"
} %>
The text field is attached to a model and in turn to a value in a database. Also, the same html that is used in index.html is also used in edit.html and hence when I try to edit the field, the default value shows up instead of the value from the database. What is the correct way to do this?
Note: I am not trying to set a default value but just a hint to what needs to be entered in the text box.
Thanks,
Raja Ram
I'd recommend using the HTML 5 placeholder attribute, and then using a jQuery plugin to make it work on older browsers (like this one, but there are many others on Google).
You can see this technique used in production here.
Try this jquery plugin
http://plugins.jquery.com/project/hint
or choose here:
http://plugins.jquery.com/plugin-tags/hint
For prototype
http://davidchambersdesign.com/autopopulating-input-fields-with-prototype/

Resources