我收到一个未定义的方法 `site_name' 用于 nil:NilClass以下错误。将不胜感激任何帮助。我正在尝试在 Site 表中列出 site_name,但我不确定如何最好地解决该问题。
class Site < ActiveRecord::Base
attr_accessible :site_id, :site_name, :region
has_many :trials, :foreign_key => "site_id"
end
class Trial < ActiveRecord::Base
attr_accessible :trial_id, :site_id, :year, :trial_type
scope :year, ->(year) { where(year: year) }
scope :trial_id, ->(trial_id) { where(trial_id: trial_id) }
belongs_to :site
end
我的控制器:
class TrialsController < ApplicationController
def index
@years = Trial.group('year').order('year DESC')
end
def trial
@trial = Trial.trial_id(params[:trial_id])
end
**def list
@list = Trial.year(params[:year]).order(:region_id).joins(:sites).where('sites.site_id' => :site_id)
结尾**
end
我的观点:
<% @list.each do |list| %>
<tr>
<td>
<%= link_to list.site.site_name, trial_trials_path(trial_id: list.trial_id) %>
</td>
<td>
<%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) unless list.trial_type.blank? %>
</td>
<td>
<%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) %>
</td>
<td>
<%= link_to list.planted_at, trial_trials_path(trial_id: list.trial_id) unless list.planted_at.blank? %>
</td>
<td>
<%= link_to list.picked_at, trial_trials_path(trial_id: list.trial_id) unless list.picked_at.blank? %>
</td>
</tr>
<% end %>