{"action":"created","issue":{"url":"https://api.github.com/repos/thoughtbot/guides/issues/398","labels_url":"https://api.github.com/repos/thoughtbot/guides/issues/398/labels{/name}","comments_url":"https://api.github.com/repos/thoughtbot/guides/issues/398/comments","events_url":"https://api.github.com/repos/thoughtbot/guides/issues/398/events","html_url":"https://github.com/thoughtbot/guides/pull/398","id":128052197,"number":398,"title":"Advocate using locals to render views","user":{"login":"mcmire","id":7371,"avatar_url":"https://avatars.githubusercontent.com/u/7371?v=3","gravatar_id":"","url":"https://api.github.com/users/mcmire","html_url":"https://github.com/mcmire","followers_url":"https://api.github.com/users/mcmire/followers","following_url":"https://api.github.com/users/mcmire/following{/other_user}","gists_url":"https://api.github.com/users/mcmire/gists{/gist_id}","starred_url":"https://api.github.com/users/mcmire/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mcmire/subscriptions","organizations_url":"https://api.github.com/users/mcmire/orgs","repos_url":"https://api.github.com/users/mcmire/repos","events_url":"https://api.github.com/users/mcmire/events{/privacy}","received_events_url":"https://api.github.com/users/mcmire/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":10,"created_at":"2016-01-22T00:14:35Z","updated_at":"2016-02-01T18:55:53Z","closed_at":"2016-01-25T15:43:00Z","pull_request":{"url":"https://api.github.com/repos/thoughtbot/guides/pulls/398","html_url":"https://github.com/thoughtbot/guides/pull/398","diff_url":"https://github.com/thoughtbot/guides/pull/398.diff","patch_url":"https://github.com/thoughtbot/guides/pull/398.patch"},"body":"It has [long] been [argued] that using instance variables to pass values\r\nto Rails views isn't the greatest of ideas: Rails twists instance\r\nvariables to do something they aren't supposed to do, and if you think\r\nabout it, it's unclear how views even have access to variables that live\r\nin a completely different scope. (Hint: they're copied from the\r\ncontroller to the view at runtime when the view is instantiated.)\r\n\r\nIn most other languages, views (or templates, as they're sometimes\r\ncalled) are essentially functions and, as such, when you're rendering a\r\ntemplate and want to give that template access to certain values, you\r\njust pass those values along as arguments.\r\n\r\nIs there a way to do this in Rails? Yes, by calling `render` explicitly\r\nand using the `locals` option. So your controller would go from this:\r\n\r\n``` ruby\r\nclass PostsController < ApplicationController\r\n  def index\r\n    @posts = Post.all\r\n  end\r\nend\r\n```\r\n\r\nto this:\r\n\r\n``` ruby\r\nclass PostsController < ApplicationController\r\n  def index\r\n    posts = Post.all\r\n    render locals: { posts: posts }\r\n  end\r\nend\r\n```\r\n\r\nThe upside to this approach is that now, views and partials act the same\r\nin terms of how they reference variables, and you can convert a view to\r\na partial easily.\r\n\r\nThe downside is that you can no longer easily assert that a view was\r\nrendered with a particular variable, but that's okay, because we don't\r\nwrite controller specs anyway.\r\n\r\n[long]: https://github.com/hashrocket/decent_exposure\r\n[argued]: http://thepugautomatic.com/2013/05/locals/"},"comment":{"url":"https://api.github.com/repos/thoughtbot/guides/issues/comments/178128888","html_url":"https://github.com/thoughtbot/guides/pull/398#issuecomment-178128888","issue_url":"https://api.github.com/repos/thoughtbot/guides/issues/398","id":178128888,"user":{"login":"jferris","id":7591,"avatar_url":"https://avatars.githubusercontent.com/u/7591?v=3","gravatar_id":"","url":"https://api.github.com/users/jferris","html_url":"https://github.com/jferris","followers_url":"https://api.github.com/users/jferris/followers","following_url":"https://api.github.com/users/jferris/following{/other_user}","gists_url":"https://api.github.com/users/jferris/gists{/gist_id}","starred_url":"https://api.github.com/users/jferris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jferris/subscriptions","organizations_url":"https://api.github.com/users/jferris/orgs","repos_url":"https://api.github.com/users/jferris/repos","events_url":"https://api.github.com/users/jferris/events{/privacy}","received_events_url":"https://api.github.com/users/jferris/received_events","type":"User","site_admin":false},"created_at":"2016-02-01T18:55:53Z","updated_at":"2016-02-01T18:55:53Z","body":"> Is this a reason to use `render locals...`? Is there a way to still access (in tests) these variables? I feel like there must be, especially if `assigns` is deprecated in Rails 5.\r\n\r\nI don't think I've recently written a test that makes an assertion on `assigns`. I test inputs and outputs to controllers in controller specs. I usually test side effects like sending emails. When testing responses, I generally just check whether a certain template was rendered or an action redirected."}}