rails中的以下方法有什么区别?

时间:2013-05-28 05:59:24

标签: ruby-on-rails

users_pathuser_path(user1)
我想前者去索引动作,后者去特定用户的show动作

3 个答案:

答案 0 :(得分:2)

您的期望是正确的:

users_path

时提供控制器操作users#index的路径

user_path(user1)提供控制器操作users#show的路径,其中params[:id]设置为user1.id

答案 1 :(得分:0)

users_path returns /users
new_user_path returns /users/new
edit_user_path(:id) returns /users/:id/edit (for instance, edit_user_path(10) returns /users/10/edit)
user_path(:id) returns /users/:id (for instance, user_path(10) returns /users/10)

了解更多详情 http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

答案 2 :(得分:0)

users_path will routes to users#index as you expected

并且

user_path(user) will redirect to users#show if method is get

It will redirect or routes to users#destroy if method is delete