내 응용 프로그램에 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"}
불행하게도 보인다. 하지만 이것이 그 문제를 해결할 수있는 유일한 방법일까요? –