Destroying a record via RJS TemplateError (Called ID for nil...)

Posted by bgadoci on Stack Overflow See other posts from Stack Overflow or by bgadoci
Published on 2010-05-30T21:19:01Z Indexed on 2010/05/30 21:22 UTC
Read the original article Hit count: 269

Filed under:
|
|
|
|

I am trying to destroy a record in my table via RJS and having some trouble. I have successfully implemented this before so can't quite understand what is not working here. Here is the setup:

I am trying to allow a user of my app to select an answer from another user as the 'winning' answer to their question. Much like StackOverflow does. I am calling this selected answer 'winner'.

class Winner < ActiveRecord::Base
  belongs_to :site
  belongs_to :user
  belongs_to :question
  validates_uniqueness_of :user_id, :scope => [:question_id]
end

I'll spare you the reverse has_many associations but I believe they are correct (I am using has_many with the validation as I might want to allow for multiple later). Also, think of site like an answer to the question.

My link calling the destroy action of the WinnersController is located in the /views/winners/_winner.html.erb and has the following code:

<% div_for winner do %>
         Selected <br/>
        <%=link_to_remote "Destroy", :url => winner, :method => :delete %>
    <% end %>

This partial is being called by another partial `/views/sites/_site.html.erb and is located in this code block:

<% if  site.winners.blank? %>

    <% remote_form_for [site, Winner.new] do |f| %>

    <%= f.hidden_field :question_id, :value => @question.id %> 
    <%= f.hidden_field :winner, :value => "1" %>
    <%= submit_tag "Select This Answer"  %> Make sure you unselect any previously selected answers.
<% end %>

<% else %>

<div id="winner_<%= site.id %>" class="votes">
    <%= render :partial => site.winners%>
</div>

<% end %>

<div id="winner_<%= site.id %>" class="votes">

</div>

And the /views/sites/_site.html.erb partial is being called in the /views/questions/show.html.erb file.

My WinnersController#destroy action is the following:

  def destroy
    @winner = Winner.find(params[:id])
    @winner.destroy

    respond_to do |format|
      format.html { redirect_to Question.find(params[:post_id]) }
      format.js
    end
  end

And my /views/winners/destroy.js.rjs code is the following:

page[dom_id(@winner)].visual_effect :fade

I am getting the following error and not really sure where I am going wrong:

Processing WinnersController#destroy (for 127.0.0.1 at 2010-05-30 16:05:48) [DELETE]
  Parameters: {"authenticity_token"=>"nn1Wwr2PZiS2jLgCZQDLidkntwbGzayEoHWwR087AfE=", "id"=>"24", "_"=>""}
Rendering winners/destroy

ActionView::TemplateError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id) on line #1 of app/views/winners/destroy.js.rjs:
1: page[dom_id(@winner)].visual_effect :fade

    app/views/winners/destroy.js.rjs:1:in `_run_rjs_app47views47winners47destroy46js46rjs'
    app/views/winners/destroy.js.rjs:1:in `_run_rjs_app47views47winners47destroy46js46rjs'

Rendered rescues/_trace (137.1ms)
Rendered rescues/_request_and_response (0.3ms)
Rendering rescues/layout (internal_server_error)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about ruby-on-rails