Rafal
Rafal

Reputation: 11

Application Record, variable in partial, Ruby on Rails 7

I have document with advertisement under _feed_ad.html in views/application folder and when I put there <% title @advert.name %> I get this error: undefined method `name' for nil:NilClass. Should I initialize variable advert somewhere? I have scaffold Advert previously, but it does not help. Something is missing.

Upvotes: 0

Views: 134

Answers (1)

Viktor Ivliiev
Viktor Ivliiev

Reputation: 1334

You really need to declare the variable @advert in the controllers where this partial was used. Or rewrite _feed_ad.htm in such a way:

<% title advert.name %>

and render

  = render 'application/feed_ad', advert: @advert

which is more preferable

Upvotes: 0

Related Questions