我在我的視圖中有以下循環顯示給定錦標賽中的所有分區。當我嘗試複製控制器中的邏輯時,將該變量傳遞給視圖,I和「未定義的方法」錯誤。任何幫助我做錯了將不勝感激。Rails邏輯在視圖中工作,但不是控制器
模型
class Tournament < ApplicationRecord
has_and_belongs_to_many :divisions
end
class Division < ApplicationRecord
has_and_belongs_to_many :tournaments
end
工作負責人&查看 控制器
def index
@tournaments = Tournament.all
end
查看
<% tournament.divisions.ids.each do |tdi| %>
<%= Division.find(tdi).name %>
<% end %>
不工作控制器&查看 控制器
def index
@tournaments = Tournament.all
@tournaments.divisions.ids.each do |tdi|
@divisions = Division.find(tdi).name
end
end
查看
<%= @divisions %>
當我嘗試第二個(我敢肯定,更正確的)實現,我得到一個「未定義的方法'分裂」錯誤在下面一行索引方法:
@tournaments.divisions.ids.each do |tdi|
最可能的@divisions是在每個循環內。範圍只是在該循環內 – DroidNoob