We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

benwdwd • 11 years ago

For me this caused a DoubleRenderError when used in conjunction with a respond_to block. Am I overlooking something?

Henrik N • 11 years ago

It should be a render like any other. Do you render or redirect after that line? Show the controller action if you like and I'll try to help.

benwdwd • 11 years ago

No, I was trying to use it with 4.1 ActionPack Variants and running into a whole sleuth of errors.

Henrik N • 11 years ago

I'd be happy to try it out in a 4.1 app if you show a minimal example of code that breaks.

Henrik N • 11 years ago

Here's one that can also take an action: https://gist.github.com/hen...

agenteo • 10 years ago

Good article! I am leaning towards using locals vs. instance variables when my app starts growing. Primarily because it plays well with hexagonal approach but being explicit in more complex scenarios is good.

You said "If you need to check for the presence of a key, defined?(item) will not work but local_assigns.has_key?(:item) will." which version of rails was that tested on?

I tested it on rails 4.2 and using defined? in the view seem to work, have a look:

https://github.com/agenteo/...

Henrik N • 10 years ago

Thank you! Two years after writing this post, I still use locals in new projects.

I don't remember what version I tried it on – I suppose it may have been Rails 3. Good to know `defined?` works on 4.2.

Henrik N • 10 years ago

It indeed seems to have been fixed sometime around 4.2: http://nithinbekal.com/post...

pyromjm • 9 years ago

what about the specs! Rspec lets you assert on `assigns` but not on passed locals hash. :/

Henrik N • 9 years ago

I very rarely write controller specs – perhaps there is something like `local_assigns` or `view.local_assigns` you could use. If not, they would probably accept a pull request for it.

Augusts Bautra • 9 years ago

I've been developing with Rails for the third year now and this approach is growing increasingly appealing. Being explicit about what variables a template or partial relies on saves a lot of headache. It also moves one towards passing views a single hash of data to work with fewer locals and this bodes well with eventually passing the data as JSON to a frontend framework like Angular or React.