开发者

Updating multiple divs w/ RJS/AJAX

开发者 https://www.devze.com 2023-01-01 01:24 出处:网络
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 th

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


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.


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 && !@question.likes.blank && @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"


In controller you need to load @question variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消