Skip to content

Replace instance variable with local variable

Richard Huang edited this page Aug 15, 2010 · 4 revisions

Please go to http://rails-bestpractices.com/posts/27-replace-instance-variable-with-local-variable

Before:


class PostsController < ApplicationController

  def show
    @post = Post.find(params[:id])
  end

end

<%= render :partial => "sidebar" %>

After:


<%= render :partial => "sidebar", :locals => { :post => @post } %>

This is a clean way nowadays


<%= render "sidebar", :post => @post %>