How to add article title to URL using link_to in Rails app?

前端 未结 1 658
慢半拍i
慢半拍i 2021-01-03 05:08

I am trying to follow answer to question

How to get link_to in Rails output an SEO friendly url?

but don\'t get the result. In my Rails 3 app I have:

相关标签:
1条回答
  • 2021-01-03 05:51

    Your link_to will be using the route from resources :articles. If you want it to use the custom route you've defined, name the route, and then use that in your link_to:

    # routes.rb
    match '/articles/:id/:title' => 'articles#show', :as => :article_with_title
    
    # articles/index.html.erb
    link_to article.title, article_with_title_path(article, :title => article.title)
    

    Also, you may want to consider looking into using friendly_id. It does this for you, but more transparently.

    0 讨论(0)
提交回复
热议问题