Forum: Rails deployment Dynamic drop down for subcategory based on 'On change' of ca

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.
57f355c4d8bab93d67dfbd1691ab0be4?d=identicon&s=25 Devi Rv (devirv)
on 2009-05-21 12:32
Hi,

I am new to ror. I want to show 'Dynamic drop down for subcategory based
on 'On change' of category' and below is the code I used. But I can make
the sub category drop doen to be filtered based on category. Please any
one suggest...

list.rhtml
----------
<table width=30%>

  <tr><td>Category Name</td>
  <td><%=
collection_select(:categories,:id,@categories,:id,:category_name,
{:onChange => "new Ajax.Updater('subcategories','update_subcategories/'
+ this[this.selectedIndex].value,{asynchronous:true,
evalScripts:true});"})%></td></tr>

   <%= render :partial => "subcategory", :locals => {:category =>
@category}%>
</table>

_subcategory.html
------------------
<tr><td>Sub Category Name</td>
  <td><%=
collection_select(:subcategories,:id,@subcategories,:id,:scategory_name)%></td></tr>


category_controller
-------------------
class CategoryController < ApplicationController

  def list
    @categories = Category.find(:all)
    @subcategories = Subcategory.find(:all)
  end

  def update_scategories
    category = Category.find(params[:category_id])
    subcategories = category.subcategories


    #render :partial => "subcategory", :locals => {:category =>
category}

    render :update do |page|
      page.replace_html 'subcategories', :partial => 'subcategories',
:object => subcategories
    end
  end
end
57f355c4d8bab93d67dfbd1691ab0be4?d=identicon&s=25 Devi Rv (devirv)
on 2009-05-22 13:48
Devi Rv wrote:
 Hi,

 I am new to ror. I want to show 'Dynamic drop down for subcategory
based
 on 'On change' of category' and below is the code I used. But I can't
make
 the sub category drop down to be filtered based on category. Please any
 one suggest...

 list.rhtml
 ----------
 <table width=30%>

   <tr><td>Category Name</td>
   <td><%=
 collection_select(:categories,:id,@categories,:id,:category_name,
 {:onChange => "new Ajax.Updater('subcategories','update_subcategories/'
 + this[this.selectedIndex].value,{asynchronous:true,
 evalScripts:true});"})%></td></tr>

    <%= render :partial => "subcategory", :locals => {:category =>
 @category}%>
 </table>

 _subcategory.html
 ------------------
 <tr><td>Sub Category Name</td>
   <td><%=
 collection_select(:subcategories,:id,@subcategories,:id,:scategory_name)%</td></tr>


 category_controller
 -------------------
 class CategoryController < ApplicationController

   def list
     @categories = Category.find(:all)
     @subcategories = Subcategory.find(:all)
   end

   def update_scategories
     category = Category.find(params[:category_id])
     subcategories = category.subcategories


     #render :partial => "subcategory", :locals => {:category =>
 category}

     render :update do |page|
       page.replace_html 'subcategories', :partial => 'subcategories',
 :object => subcategories
     end
   end
end
57f355c4d8bab93d67dfbd1691ab0be4?d=identicon&s=25 Devi Rv (devirv)
on 2009-05-25 10:53
Hi

With the below code I able to solve the above issue

view page
---------

<%= javascript_include_tag "prototype" %>

<%= collection_select :category,
   :id, @categories,
   :id, :category_name,
   { :include_blank => false},
           {:onchange => remote_function(:url =>
               {:action => 'say_hello'},
             :with => "'id=' + this.value",
             :update => "mydiv")}

%>
<div id="mydiv">
<%= render :partial => "subcategory/subcategory",
  :locals => {:category =>Category.find(:all)}%>
</div>


controller
----------
 def say_hello
     @subcategories = Subcategory.find_by_sql("select * from
subcategories where category_id="+params[:id])
     render :partial => "subcategory/subcategory" ,:id=>@id
  end
This topic is locked and can not be replied to.