这可能是一个简单的解决方案,但我很难过。
我想显示一个表格:事件名称 | 赛事运动员成绩 | 赛事运动员得分
如果我检查 pry 控制台中的数据,则一切都已正确链接。例如。@athlete.event_athletes.first.event.name
这将返回事件的正确名称。
但是,当我尝试在浏览器(即ea.event.name
)中显示数据时,我收到错误:
“/athletes/1 处的 NoMethodError 未定义方法‘名称’为 nil:NilClass”
我怀疑这可能是强参数的问题。
https://github.com/thaczuk/scoring/tree/eventScoring 请指教。
楷模
class Athlete < ActiveRecord::Base
belongs_to :competition
belongs_to :competition_category
has_many :event_athletes
has_many :events, through: :event_athletes
end
class Event < ActiveRecord::Base
belongs_to :competition
has_many :event_athletes
has_many :athletes, through: :event_athletes
end
class EventAthlete < ActiveRecord::Base
belongs_to :athlete
belongs_to :event
end
控制器
class AthletesController < ApplicationController
...
def show
@athlete = Athlete.find(params[:id])
binding.pry
end
...
private
def athlete_params
params.require(:athlete).permit(:firstname, :lastname, :gender, :competition_id, :competition_category_id)
end
end
看法
%div
%table.table
%thead
%tr
%th(width="30%") Event
%th Result
%th Score
%tbody
-@athlete.event_athletes.each do |ea|
%tr
%td= ea.event.name
%td= ea.result
%td= ea.score