Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass locals through a partial into another partial?

This is an example to illustrate the question:

app/views/posts/show.html.haml

.post
  = render partial: 'avatar', locals: { user: @post.user }
  = @post.title

app/views/shared/_avatar.html.haml

.avatar
  = render partial: 'avatar_image', locals: { user: user }
  = user.name

app/views/shared/_avatar_image.html.haml

= image_tag user.image.url

Passing the locals received by partial _avatar.html.haml on to partial _avatar_image.html.haml would eliminate the need to repeat the locals (here: { user: user }).

I know, I could use instance variables. But I would like to know if there is a way to generically pass all locals received to a underlying partial.

like image 456
branch14 Avatar asked Aug 19 '14 14:08

branch14


1 Answers

you can use local_assigns to access the locals hash passed to your partial

.avatar
  = render partial: 'avatar_image', locals: local_assigns
  = user.name
like image 138
usha Avatar answered Nov 08 '22 22:11

usha