0
現在我已經對Tour Tour表的tour_id進行了硬編碼,我該如何使這種動態?製作選擇標籤動態
(select_tag 'tournament_id',options_from_collection_for_select(Tournament.all.where(:tour_id => 1)
現在我已經對Tour Tour表的tour_id進行了硬編碼,我該如何使這種動態?製作選擇標籤動態
(select_tag 'tournament_id',options_from_collection_for_select(Tournament.all.where(:tour_id => 1)
如果我理解你的問題是正確的。
型號
class Tour < ActiveRecord::Base
has_many :tournaments
end
class Tournament < ActiveRecord::Base
belongs_to :tour
end
控制器(像這樣)
....
@tour = Tour.includes(:tournaments).find(1)
在意見
(select_tag 'tournament_id',options_from_collection_for_select(@tour.tournaments, ...)
無論如何,Tournament.all.where(:tour_id => 1)
不是軌道的方式。在不同的練習中使用鋼軌關聯(更多信息請點擊rails associations)
@tour = Tour.find(1)
@tour.tournaments # all tournaments where tour_id = 1 (tour has_many tournaments)
您需要給出更多關於您正在嘗試做什麼的背景。您是否試圖在Tournament中的每條記錄中選擇一個下拉菜單作爲選項? – 2015-03-18 22:35:01
不僅錦標賽屬於特定的巡迴賽 – 2015-03-18 22:35:41