Rob
Rob

Reputation: 2323

select_tag doesn't save the selected option

I'm new in web development & Rails. I've been struggling to understand why my form is not getting saved completely. Here is the code I'm using:

  <div class="field">
    <%= f.label :type %><br>
    <%= select_tag(:type, options_for_select([['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb']])) %>
  </div>
  <div class="field">
    <%= f.label :category %><br>
    <%= select_tag "category",
                   "<option>Appliances</option>
                    <option>Clothes and Accessories</option>
                    <option>Colours</option>
                    <option>Communication and Technology</option>
                    <option>Documents and Texts</option>
                    <option>Education</option>
                    <option>Entertainment and Media</option>
                    <option>Family and Friends</option>
                    <option>Food and Drink</option>
                    <option>Health, Medicine and Exercise</option>
                    <option>Hobbies and Leisure</option>
                    <option>House and Home</option>
                    <option>Measurements</option>
                    <option>Personal Feelings, Opinions and Experiences (adjectives)</option>
                    <option>Places: Buildings</option>
                    <option>Places: Countryside</option>
                    <option>Places: Town and City</option>
                    <option>Services</option>
                    <option>Shopping</option>
                    <option>Sport</option>
                    <option>The Natural World</option>
                    <option>Time</option>
                    <option>Travel and Transport</option>
                    <option>Weather</option>
                    <option>Work and Jobs</option>".html_safe %>
  </div>

PS: I've kept two different methods I tried to use.

Upvotes: 3

Views: 1904

Answers (3)

Kinaan Khan Sherwani
Kinaan Khan Sherwani

Reputation: 1534

use f.select instead of select_tag.

f.select(:type, [['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb'])

or if you are using form_for and passing an object then you can also do it as follows.

select_tag(:type, options_for_select([['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb']],f.object.type))

we are passing a value of type from actual object into option for select. As you are using option_for_select it expects that you send selected value as a second parameter.

Upvotes: 3

Alexis
Alexis

Reputation: 5075

For the first one, you should use f.select instead of select_tag, rails tags are helpers for generate html elements, but in this case you need an item that is linked to your form, so you use the form helper for select instead.

For the other example, i'm not entearly sure if it will work like that, but try with the same idea, you should found that the select is passed to your controllers, also use the symbol name instead the string name, meaning :category instead "category", if you want to have a phrase like "select a category...." add another option at the end with :prompt => "select a category...", hope it helps and look at Ryan Bates site, is an excellent place to learn rails

Upvotes: 0

Harsh Gupta
Harsh Gupta

Reputation: 4538

options_for_select also takes second parameter which is the selected value.

http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select

# this will show Preposition selected
options_for_select([['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb']], 'Preposition')

For future reference, please always specify Rails version while posting question.

I noticed you are using f.label, in which case you might also want to take a look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select

HTH

Upvotes: 1

Related Questions