Here my models:
class Train < ApplicationRecord
has_many :train_lines
end
class TrainLine < ApplicationRecord
belongs_to :train
end
My controller:
# controllers/train_lines_controller.rb
class TrainLinesController < ApplicationController
def index
@train_lines = TrainLine.all
end
end
And here my views:
# views/train_lines/index.html
<%= render @train_lines %>
# views/train_lines/_train_line.html.erb
<p><%= train_line.train.id %></p>
I get undefined methodid' for nil:NilClass` error
If I run Rails console I can successfully call:
$ tl = TrainLine.first
$ tl.train.id
$ 6028
EDIT
It seems it renders something, and for some reason it breaks. I get the following in the log:
Rendered collection of train_lines/_train_line.html.erb [6197 times] (5875.4ms) Rendered train_lines/index.html.erb within layouts/application (5992.4ms) Completed 500 Internal Server Error in 6008ms (ActiveRecord: 1165.4ms)
ActionView::Template::Error (undefined method `id' for nil:NilClass): 1:
<%= train_line.train.id %>
Any clue?
@train_line.train.id. - hashrocketRendered collection of ...in the server's log? - Sebastian Palma