2011-05-08 5 views
4

내 응용 프로그램에 profile 모델이 있습니다. 나는 사용자가 /profile를 통해 자신의 프로필을 볼 수 있도록하려는, 그래서 나는이 경로 생성 :리소스 (단수) 및 리소스 (복수형) 모두에 대해 Rails 라우트를 만드는 가장 좋은 방법은 무엇입니까?

resource :profile, :only => :show 

나는 또한 사용자가 /profiles/joeblow를 통해 다른 사용자의 프로필을 볼 수 있도록하려면를, 그래서이 길을 만든 :

문제가
resources :profiles, :only => :show 

, 두 번째 경우에, 나는 프로필을 찾아 사용하려는 :id 매개 변수가있다. 첫 번째 경우에는 로그인 한 사용자의 프로필 만 사용하려고합니다.

이것이 올바른 프로필을 찾는 데 사용되는 것이지만,이 작업을 수행 할 수있는 적절한 방법이 있는지 궁금합니다.

class ProfilesController < ApplicationController 
    before_filter :authenticate_profile! 
    before_filter :find_profile 

    def show 
    end 

    private 

    def find_profile 
     @profile = params[:id] ? Profile.find_by_name(params[:id]) : current_profile 
    end 
end 

편집 :이 방법의 문제점 중 하나는 내 경로입니다. profile_path에 프로필/ID 매개 변수를 전달하지 않고도 전화 할 수 없습니다. 즉, 언제든지 '/ 프로필'문자열을 사용해야 할 때마다 연결할 수 있습니다.

$ rake routes | grep profile 
    profile GET /profiles/:id(.:format) {:action=>"show", :controller=>"profiles"} 
      GET /profile(.:format)  {:action=>"show", :controller=>"profiles"} 

답변

2

귀하의 경로 :

resource :profile, :only => :show, :as => :current_profile, :type => :current_profile 
resources :profiles, :only => :show 

그런 다음 ProfilesController

class ProfilesController < ApplicationController 
    before_filter :authenticate_profile! 
    before_filter :find_profile 

    def show 
    end 

    private 

    def find_profile 
    @profile = params[:type] ? Profile.find(params[:id]) : current_profile 
    end 
end 

귀하의 Profile 모델

class Profile < AR::Base 
    def to_param 
    name 
    end 
end 

조회수 :

<%= link_to "Your profile", current_profile_path %> 
<%= link_to "#{@profile.name}'s profile", @profile %> 
# or 
<%= link_to "#{@profile.name}'s profile", profile_path(@profile) %> 

또한 : 프로필 모델 인 경우 내가 뭔가를 누락하지 않는 즉, URL`/ current_profile` 대신`/ profile`을 만드는 것처럼, 당신은

+0

불행하게도 보인다. 하지만 이것이 그 문제를 해결할 수있는 유일한 방법일까요? –

관련 문제