2012-01-11 4 views
2

thumbs_up 보석이있는 게시물에 총 투표 수를 표시하려고하는데 작동하지 않는 것 같습니다. 내가 플래시 메시지 "당신은 성공적으로 투표했습니다"하지만 내 표가 표시되지 않아요 수를 얻을thumbs_up으로 총 투표 수 표시

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

- :

여기 내 코드 뷰에서

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 

입니다.

내가 내 로그 파일에있는 것입니다 : -.

[1m [36m (0.3ms) [0m [1mSELECT의 COUNT (*) "표" "투표"FROM "voteable_id" = 12 AND "투표". ​​"voteable_type"= '포스트'와 "투표". ​​"투표"= 't'[0m 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

리디렉션 할 때 표시되지 않습니다. 리디렉션 할 때 기본적으로 새 요청을 수행하며 이전 요청의 인스턴스 변수는 더 이상 사용할 수 없게됩니다. 플래시는 session을 사용하기 때문에 작동합니다. 해결 방법 : 리디렉션 할 작업에 @votes_total을 설정하거나 redirect_to 대신 render을 사용하십시오.

+0

템플릿을 렌더링해도 볼 수 없습니다 : template => "home/index" –

+0

'render ... '위에'@votes_total = post.votes_for'를 넣어야합니다. – Mischa

+0

예 like that like –

0

는 post.votes_for @

@votes_total

변경하거나보기에 상당

대응하려고.

+0

인식 할 수없는 방법 votes_for는이 작업을 통해 얻는 오류입니다. –