Why So Scared

A blog about; Programming, Music and Random Stuff

Archive for December, 2009

select_tag / select input into my database rails

In my posts form the input to the model can only be Dog or Cat, I’ve constructed the following code and assumed it would work, however the information input into the select_tag box was never going into my database.

1
2
3
4
5
6
7
8
9
10
11
<% form_for(@post) do |f| %>

    <%= f.label :title %><br />
    <%= f.text_field :title %>

    <%= f.label :source %><br />
    <%= select_tag :source,  options_for_select([ "Dog", "Cat") %>

    <%= f.submit 'Create' %>

<% end %>

I finally got this to work by using;

1
<%= f.select(:source, options_for_select({ : Dog =>'Dog',:Cat =>'Cat'} %>

If you put the options in a hash then they will be output in a random order, to output the options in a chosen order they need to be put into an array, this left me with the following code.

View

1
2
<%= f.label :source %><br />
<%= f.select(:source, options_for_select(@source)) %>

Controller

1
@source = [['Dog'],['Cat'],]
posted by Juo in Ruby and have Comments (3)