2010-03-05 29 views
1

相關問題:Vote_fu and Ajax requestsAjax and vote_fu |在語句前缺失

我的Ajax請求似乎有問題。 我想要做的是在事件點擊投票提交一個投票,然後刷新頁面刷新頁面。

votes_controller.rb:

def create 
    @album = Album.find(params[:album_id]) 

    respond_to do |format| 
     if current_user.vote(@album, params[:vote]) 
     format.js { render :action => "create", :vote => @vote } 
     format.html { redirect_to([@album.user, @album]) } 
     #format.xml { render :xml => @album, :status => :created, :location => @album } 
     else 
     format.js { render :action => "error" } 
     format.html { render :action => "new" } 
     format.xml { render :xml => @vote.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

鏈接|爲查看專輯顯示:

<%= link_to_remote "Vote Up", 
:url => user_album_votes_path(album.user, album, 
:vote => :true, :format => :js), 
:method => :post %> 

的application.js

jQuery.ajaxSetup({ 
    'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} 
}) 

jQuery(document).ready(function() { 
    $("#votes_.album").bind('click'); 
}) 

create.js

page.replace_html "#votes_{@album.id}", 
:partial => "album_vote", 
:locals => {:album => @album} 

這是我收到以下錯誤信息:

missing ; before statement 
[Break on this error] page.replace_html "#votes_#{@album.id}", ...bum_vote", :locals => {:album => @album} 

我不知道什麼是布萊恩g在這裏錯了我一直在關注vote_fu文檔的 中的很多例子,但仍有問題。在create.js做出

http://github.com/peteonrails/vote_fu/tree#readme

一個修正

現在有另一個錯誤:

沒有錯誤已經移到了votes_controller

NoMethodError (You have a nil object when you didn't expect it! 
<br /> 
The error occurred while evaluating nil.vote): 
    app/controllers/votes_controller.rb:53:in `create' 
    app/controllers/votes_controller.rb:52:in `create' 
<br /> 
Rendered rescues/_trace (128.4ms) 
Rendered rescues/_request_and_response (0.4ms) 
Rendering rescues/layout (internal_server_error) 

這些生產線都在創建操作,看起來非常好!!

我如何得到這個工作?

方面

回答

1

嘗試改變create.js到

page.replace_html "#votes_#{@album.id}", :partial => "album_vote", :locals => {:album => @album} 

你可能已經錯過了#爲變量字符串插值。

+0

嘗試過,它仍然不起作用! 感謝SamChandra – MrThomas 2010-03-05 11:44:13

0

在我看來,在create方法中的current_user沒有得到它的值集。

如何設置current_user值?在其他插件中,他們通常將其定義爲帶有訪問器的實例方法或實例變量。

所以,也許改變的創建方法如下可能會幫助:

def create 
    @album = Album.find(params[:album_id]) 

    respond_to do |format| 
     if @current_user.vote(@album, params[:vote]) 
     format.js { render :action => "create", :vote => @vote } 
     format.html { redirect_to([@album.user, @album]) } 
     #format.xml { render :xml => @album, :status => :created, :location => @album } 
     else 
     format.js { render :action => "error" } 
     format.html { render :action => "new" } 
     format.xml { render :xml => @vote.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

你可能有current_user.vote

+0

current_user是一個幫手方法 通過添加@是行不通的。 – MrThomas 2010-03-06 16:45:30

1

解決之前錯過了@!

問題是我沒有添加before語句來刷新投票計數! 所以我做了,它的工作,以及,我將create.js更改爲create.sj.erb,我也對我的application.js文件做了一些小的更改。畢竟,我添加了一個flash[:note] = you have voted!,然後添加了一個函數來刪除閃光通知後一秒和淡出!

的人誰的興趣繼承人的代碼:

的application.js

jQuery(document).ready(function() { 
    $("#vote").bind('click', function(e) { 
    if (e.target.tagName == "DIV") { 
     $(this).find(".album_extened").toggle('blind'); 
    } 
}) 

}) 

create.js.erb

$("#vote").before('<div id="notice"><%= escape_javascript(flash.delete(:notice)) %></div>'); 
$("#vote_count").html("<%= @album.votes_for - @album.votes_against %>"); 

$(document).ready(function() { 
    setTimeout(hideFlashMessages, 1000); 
}); 

function hideFlashMessages() { 
$("#vote, #notice").append('').fadeOut(1000); 
} 

如果有人知道一個更好的,爲什麼它可以做到這一點,請前鋒!!

良好的觀看[http://railscasts.com/episodes/136-jquery][1]

[鏈接文本的讀取] [2]

不久固定的東西由於喬納森link text

[1]:http://railscasts.com/episodes/136-jquery/ 「的Rails鑄EP 136」 [ 2]:http://www.notgeeklycorrect.com/english/2009/05/18/beginners-guide-to-jquery-ruby-on-rails/「初學者公會到jQuery和Rails」 和感謝山姆幫助!