ruby-on-rails-3formtastic

How do I get the label string value from a :select box in formtastic


I have the following in a partial in a Rails 3.2 app using formtastic gem

<%= f.semantic_fields_for :bucket do |bucket| %>
                       <%= bucket.inputs do %>
                               <%= bucket.input :bucket_name, :collection => @buckets,
                               :include_blank => false %>
                               <%= bucket.input :sub_directory, :collection =>
@buckets.first.paths,
                               :include_blank => false %>
                       <% end %>
               <% end %>

right now in my controller I can get :bucket_name and the value is == to an integer, In my case I only have one item in my collection but it is giving me a value of 2. My guess is this is the ID value of the object.

it's important that I get the actual :name string value of the object that is selected in the collection. I am not sure how to do this. so let's say the item I select has a label of "my label" and it's the 3rd item in the collection. how would I grab the value "my label".


Solution

  • By default, select inputs will use the id attribute of the model as the value attribute of the <option> tags, and it ties various methods on the object for the contents of the option tag such as to_label, name, and to_s.

    You can change both with the :member_value and :member_label options respectively (these were called :value_method and :label_method in older versions.

    The details of each option are in the documentation for the select input:

    http://rdoc.info/github/justinfrench/formtastic/Formtastic/Inputs/SelectInput