Crazy Partials

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?

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

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. [email protected]

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

Fred

Thanks that helped a lot. I got it now.

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):
  5. (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. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs