rubyist
rubyist

Reputation: 3132

select tag in rails 3

I am trying to create a drop down menu and assign an onchange event on it in rails3. how would I do that.

<select name="" class="wy_inputs_selects sround">
       <% @domains.each do |record| %>
  <option value="<%= record.name %>.<%= record.tld %>" ><%= record.name %>.<%= record.tld %></option>
           <%end%>
</select>

I am converting the above code into ruby like

<%= select_tag "",:class=>'wy_inputs_selects sround',options_from_collection_for_select(@domains,"name",

But I am stuck with how to get the record.tld and record.name using the above format

Upvotes: 0

Views: 2378

Answers (2)

Francois
Francois

Reputation: 10631

try this

<select name="" class="wy_inputs_selects sround">

<% @domains.each do |record| %>
 <% @name_tld = record.name.to_s + "." + record.tld.to_s %>
 <option value="#{@name_tld}" ><%= @name_tld %></option>
 <% end %>

</select>


you can also you the rails select helper method like so (this is used inside a form)

example 1

<%= form_for @dates,:remote => true do |f| %>
    <%= f.date_select :booking_date,:onchange => "this.form.submit();" %>
<% end %>

example 2 (with default value)

<%= form_for(@image),:remote => true do |f| %>
    <%= f.select :album_id, @albums_all.map {|m| [m.name, m.id] }, :selected => @album_id %> <!-- default value, current album -->
<% end %>

remember to include this in your views/layouts/application.html.erb file, so rails will use ajax

<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

Upvotes: 2

nol
nol

Reputation: 63

I would define the name in the model for record (record.rb or domain.rb, whatever the name of your model is), something like:

def detail_name
  return self.name+'.'+self.tld
end

Then call the detail_name from within the select tag.

Upvotes: 0

Related Questions