# Rendering partials with local variables questions

**URL:** <https://www.ruby-forum.com/t/rendering-partials-with-local-variables-questions/130425>\
**Category:** Rails\
**Created:** [February 28, 2008, 4:14am UTC](https://www.ruby-forum.com/t/rendering-partials-with-local-variables-questions/130425 "2008-02-28T04:14:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [February 28, 2008, 4:14am UTC](https://www.ruby-forum.com/t/rendering-partials-with-local-variables-questions/130425/1 "2008-02-28T04:14:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [February 28, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/rendering-partials-with-local-variables-questions/130425/2 "2008-02-28T12:15:36Z")

</div>

On 28 Feb 2008, at 03:14, LAB wrote:

> 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

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [February 28, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/rendering-partials-with-local-variables-questions/130425/3 "2008-02-28T13:52:57Z")

</div>

@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!

On Feb 28, 6:14 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [February 28, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/rendering-partials-with-local-variables-questions/130425/4 "2008-02-28T14:25:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [February 28, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/rendering-partials-with-local-variables-questions/130425/5 "2008-02-28T14:00:40Z")

</div>

On 28 Feb 2008, at 12:52, LAB wrote:

> @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

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [February 28, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/rendering-partials-with-local-variables-questions/130425/6 "2008-02-28T14:26:55Z")

</div>

Mark B. wrote:

> params[:thing:id] next time.

That’s params[:thing\_id] of course.

---

<div class="post-metadata">

**Author:** ![LAB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@LAB](https://www.ruby-forum.com/u/LAB)\
**Post date:** [February 28, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/rendering-partials-with-local-variables-questions/130425/7 "2008-02-28T14:39:54Z")

</div>

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

On Feb 28, 8:26 am, Mark B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
