【问题标题】:Customize URL in rails在 Rails 中自定义 URL
【发布时间】:2014-06-07 11:36:08
【问题描述】:

我有一个使用身份验证的博客,用户可以在其中创建帖子我的帖子是通过脚手架生成的,并且希望用户在想要编辑帖子时将他们重定向到类似 localhost:3000/posts/jack-stone 的内容/the-first-post/edit 而不是 localhost:3000/posts/the-first-post/edit (jack-stone 是用户全名,the-first-post 是帖子标题(我使用 FriendlyId gem这))我怎样才能做到这一点?我的实际发帖路线是resources :posts

【问题讨论】:

    标签: ruby-on-rails ruby routes friendly-url


    【解决方案1】:

    您可能希望使用带有模式的match 语句进行配置。

    match '/posts/:user_full_name/:post_title/edit', :to => "posts#edit", :via => 'get'
    

    posts#edit 中可用的参数将是 params[:user_full_name]params[:post_title]

    延伸阅读:here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      相关资源
      最近更新 更多