2012-01-11 45 views
2

我想顯示投票與thumbs_up寶石總投票,但它似乎並沒有工作。顯示與thumbs_up總投票計數

這裏是我的代碼

def vote_up 
    begin 
     post = Post.find(params[:id]) 
     current_user.vote_for(post) 
     redirect_to :back 
     flash[:sucess] = "You have voted successfully" 
     @votes_total = post.votes_for 
    rescue ActiveRecord::RecordInvalid 
     redirect_to :back 
     flash[:error] = "You have already voted for this one" 
    end 

    end 

在視圖: -

<%="Total votes = #{@votes_total}"%> 

我得到的提示信息「您已成功投」計數,但沒有得到我的顯示票。

這是我在我的日誌文件: - 。

[1分[36米(0.3ms的)分[1mSELECT COUNT(*)FROM 「票」 WHERE 「票」 「voteable_id」 = 12, 「票」。 「voteable_type」= '郵報' 和 「票」, 「投票」= 'T'[0米0

---更新---

更新我的職位控制器與此代碼: -

def vote_up 
    begin 
     post = Post.find(params[:id]) 
     current_user.vote_for(post) 
     @votes_total = post.votes_for 
     render :template => "home/index" 
     flash[:sucess] = "You have voted successfully" 

    rescue ActiveRecord::RecordInvalid 
     redirect_to :back 
     flash[:error] = "You have already voted for this one" 
    end 

    end 

請幫忙。

回答

1

它不會顯示,因爲您正在重定向。重定向時,你基本上正在做一個新的請求,前一個請求的實例變量將不再可用。 Flash確實有效,因爲它使用了session。解決方案:在要重定向到的操作中設置@votes_total,或者使用render而不是redirect_to

+0

即使我做了渲染,我也無法看到:template =>「home/index」 – 2012-01-11 09:03:13

+0

請注意,您必須在'render ...'上方放置'@votes_total = post.votes_for'' – Mischa 2012-01-11 09:06:14

+0

yes我已經完成只有那 – 2012-01-11 09:06:58

0

嘗試改變

@votes_total

@ post.votes_for或視圖中的相應的等效

+0

無法識別的方法votes_for是我通過這樣做得到的錯誤 – 2012-01-13 13:38:05