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
編輯:其中一個用這種方法的問題是我的路線。我不可能在不傳遞配置文件/ ID參數的情況下調用profile_path
,這意味着無論什麼時候我需要使用字符串「/ profile」,只要需要鏈接即可。
$ rake routes | grep profile
profile GET /profiles/:id(.:format) {:action=>"show", :controller=>"profiles"}
GET /profile(.:format) {:action=>"show", :controller=>"profiles"}
不幸的是,它看起來像,使URL'/ current_profile'而不是'/ profile',除非我失去了一些東西。但我想這是解決這個問題的唯一方法? – 2011-05-20 03:57:44