Forum: Ruby on Rails related drop down list / ajax

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F734b6ca0eb2ebd01744224437a72498?d=identicon&s=25 Harm de Laat (Guest)
on 2006-01-13 20:19
(Received via mailing list)
Hi all,

I'm trying to create a related dropdown box:

In my view:

<select name="category" id="category">
   <option value="">Select category</option>
   <% @categories.each do |category| %>
      <option value="<%= category.id %>">
         <%= category.title %>
      </option>
   <% end %>
</select>

<select id='brand_id_container' name='brand_id_container'>
    <option value=''>No Brand</option>
    <% for brand in  @brands -%>
    <option value='<%= brand.id %>'><%= brand.title %></option>
<% end -%>
</select>

<%= observe_field("category",
   :frequency => 0.25,
   :update => "brand_id_container",
   :url => { :action => :get_brands_for_category },
   :with => "'category_id='+value") %>

In my controller:

  def get_brands_for_category
      @brands = Brand.find_all_by_category_id(@params["category_id"])
  end

Somehow it does not work. The related dropdown list does not change
contents. Does anybody have an idea why?

Regards,

Harm de Laat
F734b6ca0eb2ebd01744224437a72498?d=identicon&s=25 Harm de Laat (Guest)
on 2006-01-14 11:37
(Received via mailing list)
Anybody?
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2006-01-15 05:32
(Received via mailing list)
Try putting the second <select> inside a <div id="brand_id_container">
and
return the entire select from your action.You would need a render
partial or
a render text which spews out HTML
Vivek
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2006-01-21 15:39
(Received via mailing list)
On 1/13/06, Harm de Laat <harmdelaat@gmail.com> wrote:
>   def get_brands_for_category
>       @brands = Brand.find_all_by_category_id (@params["category_id"])
>   end
>
> Somehow it does not work. The related dropdown list does not change
> contents. Does anybody have an idea why?

Make sure you have a get_brands_for_category.rhtml file like:

<%= options_from_collection_for_select @brands, "id", "name" %>

And you'll want to turn off the layout for the get_brands_for_category
action.
This topic is locked and can not be replied to.