Skip to content

Instantly share code, notes, and snippets.

@olivierlacan
Last active May 31, 2019 18:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save olivierlacan/5784052 to your computer and use it in GitHub Desktop.
Save olivierlacan/5784052 to your computer and use it in GitHub Desktop.
How to use shorthand syntax for rendering partials in Rails

Inside of Rails views, you can call partial views (any view file named with an underscore as the first character) like this:

render partial: "partial_name"

You can also pass local variables, which is fantastic to ensure that your views & partials don't try to access data you didn't specifically hand to them from the controller.

@tylerhunt showed me this week that it was possible to avoid using instance variables (which are copied from the Rails controller to the corresponding Rails views) altogether by passing local variables to the view.

For each action in a Rails controller, the render method is always called implictely if you don't call it explicitely. How it finds a corresponding view is simply determined by the naming of your action.

If you have:

class BananaController < ApplicationController
  def dance
  end
end

Even though the dance action is entirely empty, the render method will look for a view inside of app/views/banana/dance.html.haml (I'm assuming you're using Haml here, because ERB is a pile of dung).

So now let's say we have an object we want to display information about in our 'dance.html.haml`, we could do this:

class BananaController < ApplicationController
  def dance
    @banana = Banana.new
  end
end

And in the view:

%h1 Banana Dance
%ul
  - @banana.dance_moves.each do |move|
    %li= move.name

But there's absolutely no need for an instance variable here, so instead let's just use locals:

class BananaController < ApplicationController
  def dance
    render locals: { banana: Banana.new }
  end
end

And in the view, we just change one character, that silly @:

%h1 Banana Dance
%ul
  - banana.dance_moves.each do |move|
    %li= move.name

Doesn't seem drastic, but now you have to specifically declare what you wish to pass to the view. It's a good habit, because it makes it painfully obvious for your front-end developers what is sent to the view when it is rendered. More importantly, it's a good first step in the directly of limiting what your views have access to.

We colloquially refer to them as views in Rails parlance, but really they're supposed to be "View Templates". The word template in my mind implies a very limited amount of intelligence. Display that data I gave you, and that's it. Don't try to be smart. Don't try to be conditional. It's a much better mindset when dealing with views. And it may limit a common bad tendency by avoiding the growth of conditional mold in your views.

Now let's talk about partials. Partials as opposed to templates can contain a small chunk of view

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment