【问题标题】:newbie: how to get dynamic links for current_user?新手:如何获取 current_user 的动态链接?
【发布时间】:2012-03-12 17:29:35
【问题描述】:

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

  • /users/user1/show
  • /users/user1/edit 或
  • /profiles/1/

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

【问题讨论】:

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


    【解决方案1】:

    config/routes.rb你需要添加1个简单的行:

    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阅读有关rails路线的信息

    【讨论】:

      【解决方案2】:

      实际上,我认为您在 config/routes.rb

      中需要这样的东西
      resources :users do
        resources :profiles
      end
      

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

      rake routes
      

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

      user_profile_path(@user)
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-16
        • 1970-01-01
        • 2019-11-04
        • 2016-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-23
        相关资源
        最近更新 更多