2012-02-06 3 views
1

다음 컨트롤러는 다음을 수행합니다.터미널에서 뷰가 렌더링되었지만 실제로 브라우저에서 렌더링되지 않는다고 말합니다.

투표 인스턴스 중 하나의 user_id 속성에 현재 사용자의 ID가 있으면 현재 사용자가 이미 투표 한 사실을 알리는 통지와 함께이 페이지로 리디렉션됩니다 . 그렇지 않은 경우 투표가 실행되고 views/votes/vote_up.js.erb으로 렌더링됩니다.

votes_controller.erb :

class VotesController < ApplicationController 
    def vote_up 
    @post = Post.find(params[:id]) 

    if @post.votes.exists?(:user_id => current_user.id) 
     redirect_to @post, notice: 'You already voted' 
    else 
     @vote = @post.votes.create(:user_id => current_user.id, :polarity => 1) 
     respond_to do |format| 
     format.js 
     end 
    end 
    end 
end 

보기/게시물/show.html.erb :

<div class="post-<%[email protected]%>"> 
    <h3><span class="vote-count"><%= @post.votes.count %></span>votes</h3><br /> 
    <%= link_to "Vote Up", vote_up_path(@post), :remote => true, :class => "vote-up" %><br /> 
</div> 

모든 것은 제외하고 작동 확인 사용자는 리디렉션 및 통지되지 않고 있음 나타나지 않습니다. 난 그냥 터미널이 통지를 얻을 :

캐시 (0.0ms)은 "사용자"SELECT * "사용자"WHERE "사용자" "ID"= 2 순서와 렌더링 users.created_at의 ASC의 LIMIT BY 1..

이 문제를 해결하기 위해 어떤 제안 : | : 게시물/레이아웃/응용 프로그램 (386.5ms) 내에서 show.html.erb 은 428ms (7.3ms 액티브 416.5ms보기)에서 확인 (200)을 완료? (그런데, here (어떻게)?

답변

2

Ajax를 통해 vote_up 메서드를 호출하기 때문에 리디렉션이 작동하지 않습니다. 당신이 할 때와 마찬가지로 자바 스크립트로 알림을 표시해야합니다. 사용자가 아직 투표하지 않은

당신이 뭔가를

할 수 :..

당신의 .erb.js 파일에 다음
def vote_up 
    @post = Post.find(params[:id]) 

    if @post.votes.exists?(:user_id => current_user.id) 
    @notice = 'You already voted' 
    else 
    @vote = @post.votes.create(:user_id => current_user.id, :polarity => 1) 
    end 

    respond_to do |format| 
    format.js 
    end 
end 

, 당신은 @notice@vote가 비어인지 여부에 따라 자바 스크립트를 다시 보내

<% unless @notice.blank? %> 
    // javascript to display @notice 
<% end %> 

<% unless @vote.blank? %> 
    // javascript to increase vote count 
<% end %> 
+0

또 다른'js.erb' 파일을 만드는 중입니까? 죄송합니다, 나는 레일 초보자입니다. 컨트롤러에서 어떻게 보이나요? – alexchenco

+0

아마 같은'.js.erb' 파일을 사용할 수 있습니다. – Mischa

+0

어떻게 할 수 있는지 힌트를 주시겠습니까? – alexchenco

관련 문제