How can I access the hash: locals from partial instead of local variables with the same name?

Besides a good idea or not, I would like to know how to access the locals hash from inside the partial instead of local variables with the same name?

I am trying to try to explore a potentially more efficient approach to a pattern, which I often participate in partial defaults:

:locals => { :opts => {:myvar => @myvar}} 

Then inside the partial:

 opts.reverse_merge!(defaults) 

It would be much cleaner to write (especially when the choice becomes more numerous):

 :locals => { :myvar => @myvar} 

Then inside the partial:

 opts => defaults.merge(local_hash) 
+5
source share
1 answer

You can access everything that comes with the locals hash in the rendering part by calling local_assigns . I think you will find, however, that more is coming than you expect (since Rails creates several local assignments for your convenience). Therefore, your approach may need to be tuned ... but that is how you do it.

+6
source

Source: https://habr.com/ru/post/1200698/


All Articles