【问题标题】:Named route with dynamic parameter?带有动态参数的命名路由?
【发布时间】:2012-02-02 20:31:44
【问题描述】:

我试图找出一个最基本的功能,以创建一个可以包含在我的视图和代码中但无法使其工作的命名路由

  match '/user/:username' => "profiles#show", :as => show_profile

如何在我的视图中包含以下代码:

show_profile_path

并将其链接到 /user/username (登录用户的) 我使用 devise 和 rails 3.2 并预先搜索了这个

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    link_to "cool profile", show_profile_path(username: @user.username)
    

    确保您在 Profiles_controller#show 中有正确的搜索方法

    @user = User.where(username: params[:username]).first
    

    【讨论】:

    • 没试过这个,今天试试,在查看了rails的路由文档之后,这应该可以工作,所以我标记它正确答案
    猜你喜欢
    • 2013-06-24
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    相关资源
    最近更新 更多