新手:如何获取current_user的动态链接?

时间:2012-03-12 17:29:35

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 routes

我试图弄清楚如何获得动态链接,例如

  • /用户/用户1 /显示
  • /用户/用户1 /编辑 或
  • /简档/ 1 /

我如何创建一个可以在我的视图中插入的路径,例如view_profile_path,并且包含用户的ID或用户名?

2 个答案:

答案 0 :(得分:3)

config/routes.rb

你需要添加一条简单的行:

resources :users

并获得所有这些东西

HTTP Verb   Path            action    named helper

GET         /users          index     users_path
GET         /users/new      new       new_user_path
POST        /users          create    users_path
GET         /users/:id      show      user_path(:id)
GET         /users/:id/edit edit      edit_user_path(:id)
PUT         /users/:id      update    user_path(:id)
DELETE      /users/:id      destroy   user_path(:id)

您可以在the guides

中阅读有关铁路线路的信息

答案 1 :(得分:2)

实际上,我认为你需要 config / routes.rb

这样的东西
resources :users do
  resources :profiles
end

您可以稍后通过发出命令检查REST-ful资源路由:

rake routes

通过这种方式,您可以更自然地将用户绑定到一个或多个配置文件的路线,因此您可以使用以下内容:

user_profile_path(@user)

创建指向用户个人资料的适当链接。

相关问题