sla*_*nty 2 ruby forms ruby-on-rails
我正在尝试使用以下内容获取grouped_collection_select
class User
has_many :pages, :through => pages_users
end
class Page
has_many :users, :through => pages_users
# name - String
# type - String
end
class PagesUser < ActiveRecord::Base
belongs_to :page
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
我希望下拉列表按类型分组,名称在下面.我在城市/乡村/大陆看过的例子并不像我想的那样有用.这样做的最佳方法是什么?我想我想要......
<%= grouped_collection_select(:user, :page_id, user.pages, :type, :name, ) %>
Run Code Online (Sandbox Code Playgroud)
但这显然是不正确的.
有什么想法吗?
编辑通过pages_users表显示实际关系.
这就是我最终的结果......
<%= select_tag 'page_id', grouped_options_for_select(Page.for_select) %>
Run Code Online (Sandbox Code Playgroud)
Page.for_select看起来像......
def for_select
{
'Type 1' => type1.map { |p| [p.name, p.id] },
'Type 2' => type2.map { |p| [p.name, p.id] }
}
end
Run Code Online (Sandbox Code Playgroud)
希望这有助于其他人.
归档时间: |
|
查看次数: |
2779 次 |
最近记录: |