使用简单形式将属性分配给 group_select 中的选项
Assigning attributes to options in a group_select using Simple Form
我很清楚使用 Simple Form but when it comes to grouped options I seem to be running into some issues. I found a post 将数据属性分配给传统 select 框中的选项,这解释了如何在没有简单形式的情况下进行操作:
<%= f.select :game_id, grouped_options_for_select(@consoles.map{ |console| [console.name, console.games.map{ |g| [g.title, g.id, {'data-game'=> g.title.downcase.gsub(/\s+/, "-")}] } ] }, selected_key = f.object.game_id) %>
当我尝试将其翻译成适当的语法时,出现了这个错误:
undefined method 'games' for ["PS3", []]:Array
<%= f.input :game_id, as: :grouped_select, collection: @consoles.map{ |console| [console.name, console.games.map{ |g| [g.title, g.id, {'data-game'=> g.title.downcase.gsub(/\s+/, "-")}] } ] }, group_method: :games, include_blank: "Select a game", input_html: {class: "boost__game"} %>
型号
class Game < ActiveRecord::Base
# attributes: title
has_and_belongs_to_many :consoles
end
class Console < ActiveRecord::Base
# attributes: name
has_and_belongs_to_many :games
end
好像是什么问题?
试试这个,
<%= f.input :game_id, as: :grouped_select, collection: @consoles.map{ |console| [console.name, console.games.map{ |g| [g.title, g.id, {'data-game'=> g.title.downcase.gsub(/\s+/, "-")}] } ] }, group_method: :last, include_blank: "Select a game", input_html: {class: "boost__game"} %>
我很清楚使用 Simple Form but when it comes to grouped options I seem to be running into some issues. I found a post 将数据属性分配给传统 select 框中的选项,这解释了如何在没有简单形式的情况下进行操作:
<%= f.select :game_id, grouped_options_for_select(@consoles.map{ |console| [console.name, console.games.map{ |g| [g.title, g.id, {'data-game'=> g.title.downcase.gsub(/\s+/, "-")}] } ] }, selected_key = f.object.game_id) %>
当我尝试将其翻译成适当的语法时,出现了这个错误:
undefined method 'games' for ["PS3", []]:Array
<%= f.input :game_id, as: :grouped_select, collection: @consoles.map{ |console| [console.name, console.games.map{ |g| [g.title, g.id, {'data-game'=> g.title.downcase.gsub(/\s+/, "-")}] } ] }, group_method: :games, include_blank: "Select a game", input_html: {class: "boost__game"} %>
型号
class Game < ActiveRecord::Base
# attributes: title
has_and_belongs_to_many :consoles
end
class Console < ActiveRecord::Base
# attributes: name
has_and_belongs_to_many :games
end
好像是什么问题?
试试这个,
<%= f.input :game_id, as: :grouped_select, collection: @consoles.map{ |console| [console.name, console.games.map{ |g| [g.title, g.id, {'data-game'=> g.title.downcase.gsub(/\s+/, "-")}] } ] }, group_method: :last, include_blank: "Select a game", input_html: {class: "boost__game"} %>