views:

239

answers:

3

I am successfully using RJS to implement AJAX on a page.replace.html create.js.rjs. I am attempting to update two locations instead of one and after watching Ryan Bates Railscast I am very close (I think) but have a problem in the syntax of my /views/likes/create.js.rjs file. Here is the situation:

located at /views/likes/create.js.rjs is the following code:

page.replace_html  "votes_#{ @site.id }", :partial => @like
page.replace_html  "counter", 10 - (@question.likes.count :conditions => {:user_id => current_user.id})
page[@like].visual_effect :highlight

My problem lies in the second line. The div "counter" displays the following code in the /views/question/show.html.erb page:

    <div id="counter">
        You have <%= 10 - (@question.likes.count :conditions => {:user_id => current_user.id}) %> votes remaining for this question
    </div>

From watching the screen cast I believe that my error has to do w/ the syntax of the second line. Specifically he mentions that you cannot use a local instance variable but not sure how to make the change. Thoughts?

UPDATE: Here is the error I am getting:

ActionView::TemplateError (undefined method `likes' for nil:NilClass) on line #2 of app/views/likes/create.js.rjs:
1: page.replace_html  "votes_#{ @site.id }", :partial => @like
2: page.replace_html  "counter", 10 - (@question.likes.count :conditions => {:user_id => current_user.id})
3: page[@like].visual_effect :highlight

    app/views/likes/create.js.rjs:2:in `_run_rjs_app47views47likes47create46js46rjs'
    app/views/likes/create.js.rjs:1:in `_run_rjs_app47views47likes47create46js46rjs'
    app/controllers/likes_controller.rb:8:in `create'

Rendered rescues/_trace (103.8ms)
Rendered rescues/_request_and_response (0.4ms)
Rendering rescues/layout (internal_server_error)

UPDATE:

class LikesController < ApplicationController

  def create
    @user = current_user
    @site = Site.find(params[:site_id])
    @like = @site.likes.create!(params[:like].merge(:user_id => @user.id))

    respond_to do |format|
     format.html { redirect_to @site}
     format.js
     end
  end
end

UPDATE: here is the likes form:

    <% remote_form_for [site, Like.new] do |f| %>
    <%= f.hidden_field :site_name, :value => "#{site.name}" %>
    <%= f.hidden_field :question_id, :value => @question.id %> 
    <%= f.hidden_field :ip_address, :value => "#{request.remote_ip}" %>
    <%= f.hidden_field :like, :value => "1" %>
    <%= submit_tag "^" , :class => 'voteup' %>
<% end %>
A: 

You are getting @question = nil. You should check @question is nil or @question.likes.count=0 and if so you should do (10-0) i.e.

if //your condition here 
  page.replace_html  "counter", "You have 10 votes remaining for this question"
else 
  page.replace_html  "counter", 10 - (@question.likes.count :conditions => {:user_id => current_user.id})

OR

remaining_votes= (@question && [email protected] && @question.likes.count :conditions => {:user_id => current_user.id} !=0 )? @question.likes.count :conditions => {:user_id => current_user.id} : 0
page.replace_html  "counter", "You have #{10-remaining_votes } votes remaining for this question"
Salil
A: 

In controller you need to load @question variable.

klew
Do I just need to add: `@question = Question.find(params[:question_id])`? or do I need to also affect the `@like = @site.likes.create!(params[:like].merge(:user_id => @user.id))` in someway?
bgadoci
Yes, it should be something like `@question = Question.find(params[:question_id])` but it depends on your current `params` hash which depends on your url. If your url looks like this: `myhost.com/questions/45` then `question_id` is in `params[:id]`. Your code with `@like` should work, but it also depends. If your form is created correctly, then it should be enough.
klew
I posted my form as I can't get your solution to work. See the edited question.
bgadoci
So you can get your question from `@question = @like.question`. One more think, it would be better if you assign ip address in controller, not in form: `@like = @site.likes.create!(params[:like].merge(:user_id => @user.id).merge(:ip_address => request.remote_ip)`
klew
+2  A: 

Try getting @question like this,as your question_id will be present inside params[:like] hash

 @question = Question.find params[:like][:question_id]

Anyway for double checking print your params like this so add a puts statement in create action of likes_controller.

puts params.inspect,params[:like][:question_id]

You have this error (undefined method `likes' for nil:NilClass) on line #2

Which means your @question is nil, you never declared this instance in your create action.

Hope this works for you.

Vamsi
Nice man...that worked perfectly
bgadoci
Glad to hear that :).
Vamsi