Dynamic drop down for subcategory based on 'On change' of ca


#1

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

<%= render :partial => “subcategory”, :locals => {:category =>
@category}%>

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

_subcategory.html

Sub Category Name <%= collection_select(:subcategories,:id,@subcategories,:id,:scategory_name)%>

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


#2

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

<%= render :partial => "subcategory", :locals => {:category =>

@category}%>

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

_subcategory.html

Sub Category Name <%= collection_select(:subcategories,:id,@subcategories,:id,:scategory_name)%

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


#3

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”)}

%>

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

controller

def say_hello
@subcategories = Subcategory.find_by_sql(“select * from
subcategories where category_id=”+params[:id])
render :partial => “subcategory/subcategory” ,:id=>@id
end