2014-11-06 4 views
1

이 link_to 메서드가 내 루트 경로로 리디렉션되는 이유는 무엇입니까?Link_to 메서드가 루트 경로로 계속 리디렉션 함

사용자 컨트롤러 :

def show 
    @user = User.find(params[:id]) 
    @user.comments.build 
    @comment = Comment.new 
    @user.votes.build 
    @vote = Vote.new 
end 

def vote: 
    @comment = Comment.find(params[:id]) 
    Vote.create!(voteable_id: params[:id], voteable_type: 'Comments') 
    @user = User.find(params[:id]) 
    redirect_to @user 
end 

사용자보기 :

<%= link_to "vote", vote_comment_path(:id => comment.id), method: :post, :class=> "btn %> 

Routes.rb :

post 'comments/:id/vote' => 'comments#vote', as: 'vote_comment' 
resources :users do 
    resources :comments 
    resources :ratings 
    resources :votes 
    end 
나는 사용자에게 다시 리디렉션 투표를 클릭 기다리고 있었다

, 대신 내 홈페이지로 간다.

같은 방법으로 params[:id]을 두 번 사용할 수 있습니까?

+0

당신이 UsersController # 쇼 코드를 제공 할 수있다? – emaxi

+0

경로 파일을 표시하십시오. – Substantial

+0

'redirect_to @ user'를'redirect_to user_path (@user)'로 변경하십시오. –

답변

1

당신은

vote_comment_path(:id => comment.id) 

params[:id]으로 검색하는 방법에 comment.id 통과를 호출 할 때.

params[:id]의 값이 다음 comment.id 인 경우 :

def vote 
    # ... 
    @user = User.find(params[:id]) # look up user by comment.id - BAD! 
    redirect_to @user    # wrong user! 
end 

당신은 코멘트 ID가 아닌 사용자 ID로 사용자를 찾고 있습니다. 현재 사용자가 사용자 1하지만 댓글이 코멘트 5 경우

는, 당신은 사용자 5, 주석과 같은 ID를 가진 사용자로 리디렉션되고있다. 내가 추측하고있어

다시 루트를 덤핑 일부 권한 검사가 있습니다 (예를 들면. 사용자 1사용자 5 동안 기록에서 볼 수 없습니다). 로그를 확인하여 리디렉션이 발생한 위치를 확인하십시오.


관련 될 수 있지만 구축 할 수있는 적절한 방법은 다음과 같이이다 :

def show 
    @user = User.find(params[:id]) 
    @comment = @user.comments.build 
    @vote = @user.votes.build 
end 
+0

아하이 봐요. 현재 사용자로 리디렉션하는 방법을 제안 하시겠습니까? –

+1

'redirect_to current_user' – Substantial

관련 문제