[size=large]分类表:
create_table :categories do |t|
t.string :name
t.integer :parent_id [color=red]#用于存储该分类的子分类[/color]
t.timestamps
end
add_index :categories, :name
end
商店表:
create_table :shops do |t|
t.integer :user_id,
t.integer :category_id,
t.text :description
t.timestamps
end
end
我的问题是:
在给商店选择一个分类时,首先是选择一个行业的分类 如:计算机,再就是选择 计算机下的--硬件 或 软件 分类了。
我现在只能选择第一级别的分类(计算机),而 他对应的子分类怎么都想不出来?
第一级别的分类我是这样实现的:
<% form_for @shop do |f| %>
-
商店名称:
<%= f.text_field :name%>
[color=brown] <%= f.collection_select :category_id, Category.find(:all), :id, :name, {:prompt => '行业类型:'} %>[/color]
#应该在这里再加一个选择列表,可是怎么弄???
<% end %>
代码仅是为了说明情况,不要受干扰。
各位大哥,支援一下。
[/size]
我的分数被扣没了,等有了一定嘉奖。
[b]问题补充:[/b]
恢复: 夜鸣猪 (初级程序员) 2009-02-23
你给我复制的代码我也见过。
我想要一个二联机的下拉列表框
就像你给出的列子一样 第一个列表框是省份,选择后,在 第二个下拉框中选择对应的城市。
这个 remote_function 怎样才能返回值(即@cities),还有update更新的那个部分的数据?
能给讲解一下?
[b]问题补充:[/b]
你说的这些我明白了,但现在唯一做不到的是 :with => "'province_id='+value" 的province_id 无法得到。
是不是有别的写法?
[b]问题补充:[/b]
我在views下:
<%= f.select :province_id, @provinces , {:include_blank => true},
{:onchange => remote_functi[size=large][/size]on(:update => 'city_select', :method => 'get',
:with => "'partial=select",
:url => {:controller => :cities, :action => :index, [color=red]:province_id => :value[/color]})}
%>
<%= f.select (:city_id, []) %>
在cities controller下:
def index
if params[:province_id]
@cities = City.find(:all, :conditions => ["province_id = ?", params[:province_id]])
else
@cities = City.find(:all)
end
respond_to do |format|
format.html { render :partial => params[:partial]}
format.xml { render :xml => @cities }
end
end
#_select.html.erb中:
<%= select(:user, :city_id, @cities.collect {|c| [c.name, c.id]}) %>
现在的问题是 :value的值去不出来!我这样实验的:用固定数值1 能成功。就说:value的值我不会取。你是怎么获取的???
..
..
:url => {:controller => :cities, :action => :index, [color=red]:province_id => 1[/color]})}