0

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

2 Answers 2

2

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 %>
Sign up to request clarification or add additional context in comments.

7 Comments

SO can I assign the onchange event handler to select tag like {:onchange => "show_spinner('"+"#{record.domainkey}"+"');"}
yes you can use it like this, select_tag :variable, options_from_collection_for_select(:all, :id, :name), :onchange => 'your_onchange_handler()' (please vote if it helps :) )
so wot does this :all :id and :name variables contain??I was trying to write the above code as <%= select_tag "",:class=>'wy_inputs_selects sround',options_from_collection_for_select(@domains,"name") But got stuck with these options
How can I implement the above code in ruby using select_tag :variable, options_from_collection_for_select(:all, :id, :name), :onchange => 'your_onchange_handler()'.. I am just learning ruby. Please help
Basically I am trying to implement <%= select_tag "",:class=>'wy_inputs_selects sround',options_from_collection_for_select(@domains.collect|records|,[records.name.records.tld,records.name.records.tld]))
|
0

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.

1 Comment

In my controller I have done as @domains = Domains.by_userid(:key => session[:wy_main_userid]).So @ domains would contain name and tld values which I need to get in select tag in view page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.