views:

19

answers:

1

I created a drop down box in my form using select_tag:

<%= select_tag(:warning, options_for_select([['None', 1], ['Medium', 2], ['High', 3]], 1)) %>

Now I want to display the corresponding text for the value they select rather than the id in my show.html.erb, so it displays 'None' instead of 1. I am new to this and can't quite figure it out. Right now I am just using the default scaffold code and that displays the id:

<%= @standing.warning %>

Thanks...

+1  A: 

Have you tried inverting the options so that ['None', 1] becomes [1, 'None']?

Update your Standing model:

class Standing
  @@warning_labels = { 1 => 'None', 2 => 'Medium', 3 => 'High' }

  def warning_str
    @@warning_labels[@warning]
  end
end

In show.rb:

<%= @standing.warning_str %>

OR

In your standing_helpers.rb:

def warning_str(warning_id)
  warning_labels = { 1 => 'None', 2 => 'Medium', 3 => 'High' }
  warning_labels[warning_id];
end

In show.rb:

<%= warning_str(@standing.warning) %>
meagar
The form works great. What I am trying to do is make the index.html.erb and show.html.erb display the text instead of the number, and I am not even sure where to begin. Searches aren't turning up much.
Rob
@Rob Sorry, misread that as your `<selects>` where showing numbers instead of names
meagar
Oh no problem. I just hate when something this simple seems so hard when you are first learning :)
Rob
Where do I put the class?
Rob
You should add that info to your model, which I assumed was called `Standing`. If that is indeed the name of your model, it will be in `apps/models/standing.rb`
meagar
Awesome! This definitely appears to work, but I just discovered that no matter what I select on my NEW or EDIT forms, it's not updating the record. It's always 1 no matter what I do. Should I post that as another question?
Rob
@Rob I would make a second question, yes.
meagar