Maciek
Maciek

Reputation: 35

Passing variable to partial

I want to render a partial for each offer. I have the following code:

offers/index.html.haml:

- @offers.each do |o|
  = render 'offershort', locals: {offer: o}

offers/_offershort.html.haml:

= link_to offer.name, offer_path
= offer.description

I get an error:

undefined local variable or method `offer'

How to do it properly?

Upvotes: 0

Views: 3653

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176352

If you use the short version, you need to drop the locals option

= render 'offershort', offer: o

otherwise

= render partial: 'offershort', locals: { offer: o }

Upvotes: 6

Related Questions