以下の nil:NilClassエラーに対して未定義のメソッド `site_name' を受け取りました。助けていただければ幸いです。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 %>