views:

18

answers:

1

In a Rails view, I'm trying to show a <select> drop-down list for a number of different string fields with restricted values.

I've been trying to do this with a partial (below), but the current value is not being selected in the <select> list.

  • Is it possible to do this in a partial? If so, how?
  • Is there a better approach to take?

edit.html.erb:

<% form_for(@my_class) do |f| %>
  <%= render :partial => "select", :locals => { :attribute_name => :blah, :f => f } %>
<% end %>

_select.html.erb:

<p>
  ...
  <%= f.label attribute_name %><br />
  <%= f.select attribute_name, [:option_a,:option_b,:option_c], { :selected => attribute_name } %>
  ...
</p>
A: 

I believe the selected option checks based on the value, not the name of the attribute.

This may work for you, but I have not tested it out:

<%= f.select attribute_name, [:option_a,:option_b,:option_c], { :selected => @my_class.send(attribute_name) } %>
Alan Peabody
@Alan Thanks. This is more convoluted than I expected, but if it works, it works. Hopefully I'll try it later this week.
Alison