分组集合在rails中选择

时间:2014-03-24 18:29:10

标签: ruby-on-rails ruby

我正在为注册表单开发动态下拉菜单的早期阶段。

我有四个模特。

City
has_many :burroughs
has_many :hoods

Burrough
belongs_to :city
has_many :hoods

Hood
belongs_to :city
belongs_to :burrough

User
Belongs_to :city
Belongs_to :burrough (this can be nil, or needs to be able to be nil)
Belongs_to :hood

我的目标是创建一个下拉菜单选择。纽约(城市)分为burroughs(即布鲁克林),然后分为引擎盖(即Park Slope,Williamsburg)。

对于另一个城市,我想跳过burrough,直接从城市(波士顿)到引擎盖(剑桥)虽然我还没有那么远。

这是我的表格

create_table "hoods", force: true do |t|
t.string   "name"
t.integer  "burrough_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer  "city_id"
end

create_table "burroughs", force: true do |t|
t.string   "name"
t.integer  "city_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "cities", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string   "name"
end

create_table "users", force: true do |t|
t.string   "name"
t.string   "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string   "password_digest"
t.string   "remember_token"
t.boolean  "admin",              default: false
t.string   "image_file_name"
t.string   "image_content_type"
t.integer  "image_file_size"
t.datetime "image_updated_at"
t.string   "city_id"
t.string   "hood_id"
t.string   "burrough_id"
end

我一直在关注RailCast 88而且我已经走到了这一步:

<div class="field">
    <%= f.label :city_id %><br>
    <%= f.collection_select :city_id, City.order(:id), :id, :name, include_blank: true %>
</div>
<div id="neighbselect">
    <%= f.label :burrough_id %><br>
    <%= f.grouped_collection_select :burrough_id, City.order(:name), :burroughs*, :name, :id, :name, include_blank: true %>
</div>
<div id="neighbselect">
    <%= f.label :hood_id %><br>
    <%= f_collection_select :hood_id, Hood.order(:name), :id, :name, include_blank: true %>
</div>

我的问题是1)*那里的星号。我需要将burroughs定义为一种方法,但我在哪里做,以及我将其定义为什么?我不确定因为

2)我在阅读grouped_collection_select语句时遇到了困难。这里发生了什么?在grouped_collection_select之后:burrough_id是什么?基于名称排序的城市的不同的burroughss?然后我有未定义的方法,然后:name,:id。

我很欣赏这一点。我很难过!谢谢你们

修改 我已经走得更远了。我把它添加到我的用户控制器......

def burroughs
@burroughs = Burrough.all  
end

我似乎还没有得到纽约市下面​​的所有bur ..检查图像......

Drop-Down http://shiftedrecording.com/shiftedaudio/TippedTest/images/Screen%20Shot%202014-03-24%20at%202.55.26%20PM.png

1 个答案:

答案 0 :(得分:0)

如果您遇到类似的问题,请确保您有rake:db migrate sit

相关问题