[英]Rails alias in routes.rb
這是在我的routes.rb文件中:
resources :users, :only => [:create] do
get 'search'
member do
get 'images'
get 'followers'
get 'following'
post 'follow'
delete 'follow'
end
end
但我也希望有人能夠調用/ self / feed和/ self / comments(其中注釋是資源),而self則是會話中的ID。 我該如何做而不重復自己?
謝謝
如果ID已在會話中,則無需將其作為URL參數傳遞。 考慮傳遞會話ID的危險:如果傳遞的ID 不是會話ID,會發生什么情況? 您將如何處理?
對於您的評論路線,您應考慮以下內容:
# config/routes.rb
resources :comments
然后,在comments控制器中,查詢所有id為session的評論:
# app/controllers/comments_controller.rb
def index
@comments = User.find(session[:id]).comments // assuming that the session `id` variable is the identifier for a `User` model
end
因此,訪問會話注釋所需要做的就是訪問/comments
路徑。
編輯:
要通過別名路徑/self/comments
訪問會話的用戶注釋,您可以創建一個命名匹配路由:
# config/routes.rb
match 'self/comments' => 'user#self_comments'
然后,你的動作添加到您的comments_controller.rb
為self_comments
。 您大概可以重用您的注釋index.html.erb
模板以呈現結果:
# app/controllers/comments_controller.rb
def self_comments
@comments = User.find(session[:id]).comments
render :template => 'comments/index'
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.