# Crazy Partials

**URL:** <https://www.ruby-forum.com/t/crazy-partials/135103>\
**Category:** Rails\
**Created:** [April 17, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/crazy-partials/135103 "2008-04-17T22:13:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 17, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/crazy-partials/135103/1 "2008-04-17T22:13:54Z")

</div>

I have this in a view:

 \<%= render :partial =\> "new", :locals =\> { :game =\> @game, :action =\> action, :count =\> count } %\> \<% elsif action.hit %\> \<%= render :partial =\> "hit", :locals =\> { :game =\> @game, :action =\> action, :count =\> count } %\> 

This in _new.html.erb  
\<%= link\_to\_remote “hit”, :url =\> { :action =\> “add\_hit”, :id =\>  
action.id, :player =\> @game.players[count] }, :update =\>  
"action_#{action.id}" %\> \<%= link\_to\_remote “skip”, :url =\> { :action  
=\> “add\_skip”, :id =\> action.id, :player =\>  
@game.players[count] }, :update =\> “action\_#{action.id}” %\>

This in partial _hit.html.erb  
\<%= link\_to\_remote “(hit)”, :url =\> { :action =\> “remove\_hit”, :id =\>  
action.id, :game =\> @game, :count =\> count, :player =\>  
 @game.players[count] }, :update =\> "action_#{action.id}" %\>

This in _skip.html.erb  
\<%= link\_to\_remote “(skip)”, :url =\> { :action =\> “remove\_skip”, :id  
=\> action.id, :game =\> @game, :count =\> count, :player =\>  
 @game.players[count] }, :update =\> "action_#{action.id}" %\>

This in my controller

def add\_hit  
@action = Action.find(params[:id])  
Player.find(params[:player]).actions \<\< @action  
@action.add\_hit  
render :partial =\> “hit”, :locals =\> { :game =\>  
Game.find(params[:game].id), :action =\> @action, :count =\>  
params[:count] }  
end

def remove\_hit  
@action = Action.find(params[:id])  
@action.remove\_hit  
render :partial =\> “new”, :locals =\> { :game =\>  
Game.find(params[:game].id), :action =\> @action, :count =\>  
params[:count] }  
end

and ditto for skips

Yet I’m still getting nil errors all over the place. Have I not passed  
the locals correctly? What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 17, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/crazy-partials/135103/2 "2008-04-17T22:23:52Z")

</div>

On 17 Apr 2008, at 21:12, edberner wrote:

> and ditto for skips
> 
> Yet I’m still getting nil errors all over the place. Have I not passed  
> the locals correctly? What am I doing wrong?  
> Well it’s almost impossible to say for sure, because you haven’t told  
> us where the error occurs, but for example in \_hit.html.erb you’re  
> using @game, but in add\_hit you don’t seem to have defined @game (did  
> you mean to just write game ?). The same is true for the remove\_hit  
> action and \_new.html.erb

Fred

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 17, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/crazy-partials/135103/3 "2008-04-17T22:37:27Z")

</div>

Here’s the error when I click from “hit” from the view  
Here’s what it passes (/games/remove\_hit/153?  
count=2&game=7&player=9)  
def add\_hit  
@action = Action.find(params[:id])  
@game = Game.find(params[:game]) #looks like it can’t find game  
Player.find(params[:player]).actions \<\< @action  
@action.add\_hit

render :partial =\> “hit”, :locals =\> { :game =\> @game, :action =\>  
@action, :count =\> params[:count] }  
end

activerecord::recordnotfound in gamescontroller#add\_hit  
couldn’t find game without an id  
rails\_root: /users/ellis/desktop/mlp

On Apr 17, 4:23 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 18, 2008, 9:50am UTC](https://www.ruby-forum.com/t/crazy-partials/135103/4 "2008-04-18T09:50:33Z")

</div>

Like I said before you seem to be using @game without having set it.

Fred

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 18, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/crazy-partials/135103/5 "2008-04-18T18:07:12Z")

</div>

Thanks that helped a lot. I got it now.

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 18, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/crazy-partials/135103/6 "2008-04-18T17:15:25Z")

</div>

I believe that Fred is right about the source of your problem.

A few other comments that might help:

1. If you’re going to pass @game into the partial as a local then  
you should refer to it as game, not @game.
2. If you’re using rails 2.x, then use dom\_id(@action) for the td  
declaration: 
3. Since the params appear to be the same, you could simplify your  
code a lot by calculating the name of the partial and then calling  
render :partial once.
4. Combine it all into something like this (note – dom\_id is used  
by content\_tag\_for):

(controller)  
@partial\_name = case  
when action.hit  
‘hit’  
when action.miss  
‘miss’  
else  
‘new’

(view)  
content\_tag\_for :td, @action, render(:partial=\>  
@partial\_name, :locals=\>{:game =\>  
@game, :action =\> action, :count =\> count })

On Apr 18, 3:43 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
