views:

791

answers:

3

I have a partial:

'profiles/_show.html.erb'

that contains code like

<%= @profile.fullname %>

I'm trying to render the partial but I'm not sure how to pass the @profile. I tried using local but apparently it sets 'profile' on my partial instead of '@profile'.

<%= render :partial => 'profiles/show', :locals => {:profile => @app.profile} %>

Is there anyway to pass it as @object instead of object, or is it designed this way?

A: 

You could always do @profile = profile in the partial.

Jim
+5  A: 

Why is it so important that you use an instance variable(variables who's names begin with '@', eg: @object) in your partial? It's not a good habit to get into. Using instance variables in partials complicates the control flow, which facilitates bugs and makes reuse of partials more difficult. This blog post explains the problem a little more in depth.

Really you have two options. The first option is the suggested solution.

  1. Change all instance variables to a local variable and pass it to the partial with the locals argument of render.

  2. Set the instance variable before the partial is rendered. Partials have access to all the instance variables that your controller sets.

Again instance variables in partials are bad. You should never set instance variables just because your partials are already written to use them. Rewrite the partial instead.

EmFi
A: 

this is more simple

<%= render :partial => "profiles/show", :collection => @profiles %>

on partial _show.html.erb

<%= profile.fullname %>

hope helped

Kuya