- new.rhtml
New pr_cate
<%= javascript_include_tag 'prototype' %>
<% form_for(@pr_cate) do |f| %>
<%= f.error_messages %>
<%=f.select(:id, @pr_cates.map {|u| [u.name,u.id]},
{ :onchange => remote_function(
:with => "'id='+value",
:method => "get",
:update => 'next_select',
:url => { :action => :new } ) }
)%>
...
- controller
def new
if params[:id]
@pr_cates = PrCate.find(:all, :conditions => ["parent_id = ?", params[:id]])
else
@pr_cates = PrCate.find(:all, :conditions => ["parent_id = ?", "0"])
end
@pr_cate = PrCate.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @pr_cate }
end
end
- _test.rhtml
<%= select("pr_cate","id", @pr_cates.map {|u| [u.name,u.id]}) %>
new前台界面中的情况是
第一个下拉项和第二个下拉项中的内容都来自
@pr_cates = PrCate.find(:all, :conditions => ["parent_id = ?", "0"])
第一个下拉项的onchange对第二个不起作用。