2011-01-20 2 views
2

일부 중첩 된 리소스 라우팅에 문제가 있습니다. 내가하려는 것은 편집 목적으로 사용자의 프로필 페이지에 링크하는 것입니다. 어느와 오류 밖으로레일 3 - 중첩 된 리소스 라우팅 - 일대일 관계

<%= link_to "Edit Profile", edit_user_profile_path(current_user) %> 

: 내 routes.rb 파일에서

No route matches {:action=>"edit", :controller=>"profiles", :user_id=>#<User id: 1, email: "EDITEDOUT", hashed_password: "EDITEDOUT", created_at: "2011-01-20 18:30:44", updated_at: "2011-01-20 18:30:44">} 

,이 같은 같습니다 내 관점에서 다음과 같이 작성한다

resources :users do 
    resources :profiles, :controller => "profiles" 
end 

내 레이크을 확인 경로를 선택하면 유효한 옵션으로 표시됩니다.

edit_user_profile GET /users/:user_id/profiles/:id/edit(.:format) {:action=>"edit", :controller=>"profiles"} 

수동으로 탐색 할 수 있습니다. 나는이 문제를 추적하는 몇 가지 문제가 봤는데, 어쨌든

class ProfilesController < ApplicationController 
    def edit 
    @user = current_user 
    @profile = current_user.profile 
    end 

    def update 
    @user = current_user 
    @profile = current_user.profile 


    respond_to do |format| 
     if @profile.update_attributes(params[:profile]) 
     format.html { redirect_to(orders_path, :notice => "Your profile has been updated.") } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @profile.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 
end 

: 좋은 방법은 여기 내 컨트롤러의 증거입니다. 모든 포인터가 도움이됩니다. 내 DB 디자인을 위해 프로필은 일대일 관계로 사용자에게 속합니다. 나는 그것이 새로운 눈의 집합을 알지 못해서 뭔가 신빙성이 있기를 바랄뿐입니다.

답변

2

경로를 자세히 살펴보면 :user_id:id이 모두 표시됩니다. 후자는이 경우 사용자 프로필을 나타냅니다. 이제

edit_user_profile_path(current_user, @profile) 

이 레일은 첫 번째 인수를 사용합니다 : 특정 프로파일, 당신은 사용자와 같은 귀하의 링크에서 프로파일을 모두 지정해야한다는 원하는 레일을 이야기하기 위해

(current_user) 에 대한 두 번째 인수 (@profile).

+0

감사합니다. 톤 폰 콘래드! 도움에 정말 감사드립니다. – Kombo