rendering partials with local variables questions

Hello ... I am using the acts_as_ratable plugin, but am trying to tweak it a bit ...

I have this code in my view: <%= render :partial => "ratings/rate", :locals => { :asset => current_user, :thing_id => @thing.id } %>

And in my Ratings controller I have: def rate     return unless logged_in?

    rateable = @rateable_class.find(params[:id])     thingID = thing_id

    # Delete the old ratings for current user     Rating.delete_all(["rateable_type = ? AND rateable_id = ? AND user_id = ? AND errand_id = ?", @rateable_class.base_class.to_s, params[:id], @current_user.id, thing_id])     rateable.add_rating Rating.new(:rating => params[:rating], :user_id => @current_user.id, :errand_id => thing_id)

    render :update do |page|       page.replace_html "star-ratings-block-#{rateable.id}", :partial => "rate", :locals => { :asset => rateable, : thing_id => thingID}       page.visual_effect :highlight, "star-ratings-block- #{rateable.id}"     end

  end

in the RJS file I have: page.replace_html "star-ratings-block-#{current_user.id}", :partial => '/ratings/rate', :locals => { :asset => current_user, :thing_id => @thing.id }

When I click on a rating, which renders the partial I get this error:

NameError (undefined local variable or method `thing_id' for #<RatingsController:0xb7b40f48>):

Why isn't the local variable I am passing from the partial available to me in my controller?

Thanks for any insight!!!

LAB

Hello ... I am using the acts_as_ratable plugin, but am trying to tweak it a bit ...

I have this code in my view: <%= render :partial => "ratings/rate", :locals => { :asset => current_user, :thing_id => @thing.id } %>

And in my Ratings controller I have: def rate    return unless logged_in?

   rateable = @rateable_class.find(params[:id])    thingID = thing_id

Are you sure the error doesn't occur here ? thing_id seems to be defined nowhere.

Fred

@thing.id is defined in the same view that I am rendering the partial in ... just above my render :partial code. :thing_id, is a new local variable I was trying to pass through the partial. So ......

<%= render :partial => "ratings/rate", :locals => { :asset => current_user, :thing_id => @thing.id } %>

I am passing :thing_id = a number, lets say 4, and I want to use that number 4 in the Ratings controller. Basically, I'm just trying to get this information up to the Ratings Controller from the view level. Maybe there is another way? But I still think this should work, and I probably have some sort of syntax wrong?

Thanks for any help!

@thing.id is defined in the same view that I am rendering the partial in ... just above my render :partial code. :thing_id, is a new local variable I was trying to pass through the partial. So ......

In the source that you pasted you were access thing_id right at the
top of the controller. Is that the line throwing the error ?

Fred

LAB wrote:

<%= render :partial => "ratings/rate", :locals => { :asset => current_user, :thing_id => @thing.id } %>

I am passing :thing_id = a number, lets say 4, and I want to use that number 4 in the Ratings controller. Basically, I'm just trying to get

Remember that the flow is:

Controller action Render view Send view to browser

So, your *instance* variables you set in the controller are visible to your view. Anything set in the view can only be used in the view before it is sent to the user.

If you want a value to pass to the browser then back to the controller when a link or button is clicked, then you can add the association to the options in an appropriate link_to or button_to call in the view (or use a hidden field). The value can then be used in the next controller action. So, if you set :thing_id in a link_to, you can see it as params[:thing:id] next time.

Mark Bush wrote:

params[:thing:id] next time.

That's params[:thing_id] of course.

Thanks, Mark! That sounds like my problem. I didn't understand that the :locals in the :partial can't really be used by the controller. Still not intuitive to me ... but okay!

Fred - the error is at the top of my controller:

NameError (undefined local variable or method `thing_id' for #<RatingsController:0xb7b40f48>):/app/controllers/ ratings_controller.rb:9:in `rate'

line 9 ... which is: thingID = thing_id

This is where I am first trying to use the local variable thing_id in the Ratings Controller.

If leveraging the :locals can be done, that would have been much prettier, but I will try passing the thing_id up to the Ratings Controller with a link_to or a form.

Thanks!!! LAB