Partial :object vs instance var

  -- Idiomatic Rails developers use a variable named after the template (article in this instance). In fact, it’s normal to take this a step further. If the object to be passed to the partial is in a controller instance variable with the same name as the partial, you can omit the :object parameter.

OK, so what that says is if I have a controller Company with an action aboutus and an instance var of @contactInfo (a hash), and I have a view called aboutus.rhtml which renders a partial _contactInfo.rhtml (same name as the instance var this time), then the render command can simply be this:

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

Instead of having to write this:

   <%= render :partial => "contactInfo", :object => @contactInfo %>

You don't need to write that. you get that for free with <%= render :partial => "contactInfo" %> if @contactInfo exists. You only need it if you're doing <%= render :partial => "contactInfo", :object => @someOthercontactInfo %>

So now we have a partial using the code contactInfo['phone'] instead of @contactInfo['phone'].

If you ask me, the fact that we required the instace var to be named contactInfo, and the partial to be named contactInfo and then the local variable to be named contactInfo has created a higher degree of interdependence than if we just used the instance var directly in the partial.

-if you use the ivar in the partial and you change the ivar in the controller, you have to change the ivar everywhere. -if you don't, but you use <%= render :partial => "contactInfo" %> and you change the ivar, you just have to add :object -if you don't and you use <%= render :partial => "contactInfo", :object => @contactInfo %> then you just change the name there Change in one place vs change in 23 places

But as far as I'm concerned that's a relatively modest convenience. Using the ivar means that you can't suddenly decide that you'd like to show a list of companies, since <%= render :partial => "contact_nfo", :collection => @contact_infos %> won't work. It means that if that if you need to reuse that partial in some other place, eg a page where the 'natural' instance variable is @company then you have to create @contact_info = @company.contact_info rather then <%= render :partial => "contactInfo", :object => @company.contact_info %>

So yes, there's a dependance between the name of the partial and the local variable that is materialised in that partial and there's one between the name of the instance variable and the name of the partial that will just work magically with <%= render :partial => "contactInfo" %> but those 2 dependencies are separate. by using the ivar in the partial you tie both of those together and limit the reusability of that partial.

Fred