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