0

I have this controller:

  def index
    @disclosures = Disclosure.where(:user_id => current_user.id)

    respond_to do |format|
      format.html{}
      format.js{}
    end

  end

and with the help of the good folks at StackOverflow I am now able to get my HAML to point to the partial like this:

  = render :partial => "/influencers/disclosures/shared/list"

but this partial throws and exception:

-if disclosures.empty?
  .alert.alert-info
    %p=(no_disclosures_message || (t "influencers.influencer_dashboard.disclosures.no_disclosures"))
%table.table.influencer-disclosures
  %tbody
    -disclosures.each do |disclosure|
      =render "influencers/disclosures/shared/row", :disclosure => disclosure

saying that:

undefined local variable or method `disclosures' for #<#<Class:0x133ca8a58>:0x133ca25e0>

But how can this be? I just queried for that disclosures object in my controller. Any idea why this is happening and how to fix it?

Thanks!!

GeekedOut
  • 16,905
  • 37
  • 107
  • 185

1 Answers1

4

You need to put an @ in front of disclosures. This is how the controller passes variables to the view.

-if @disclosures.empty?

and

-@disclosures.each do |disclosure|

Update

Another way to fix this is the change your render call. This will make it backwards compatible with other call sites of the same partial.

render :partial => "/influencers/disclosures/shared/list", :locals => {:disclosures => @disclosures}
CambridgeMike
  • 4,562
  • 1
  • 28
  • 37
  • thanks, but are you sure? There is other code in the code base which is using those partials just the way they are. Is it possible to use those partials as they currently are? – GeekedOut Apr 24 '12 at 21:19
  • just tried it, now I get this error: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.empty? – GeekedOut Apr 24 '12 at 21:23
  • Ah, I just re-read your question. Please refer to the "Update" above. – CambridgeMike Apr 24 '12 at 21:25
  • yeah the update you made worked! thank you - SO will allow me to accept your answer in 4 min and I'll do that then :) – GeekedOut Apr 24 '12 at 21:26
  • 1
    No worries. Do you understand how it works? Any @ variables you set in your controller are passed to the view. If you subsequently render a partial in that view the @ variables do not get passed along. If you would like to pass variables to partials you do it through the :locals parameter. – CambridgeMike Apr 24 '12 at 21:27
  • I see - I am kind of new to Ruby and lots of it is still over my head. But your explanation is helping me get it! :) – GeekedOut Apr 24 '12 at 21:44