ruby-on-railsurlroutes

Changing underscores to hyphens in URLs Ruby on Rails


I need to change underscores to hyphens for SEO of our to-do checklist page in Ruby on Rails project. The relevant line in routes.rb file is below:

get 'to_do' => 'current_to_do_items#landing', :as => :to_do

I tried to change it to:

get 'to-do' => 'current_to_do_items#landing', :as => :to-do

but in this part "as => :to-do" do is detected as a key word, could you please advise how should I name it?


Solution

  • :as option generates urls helpers, even if you change it to as: "to-do" your helpers will not change: to_do_path and to_do_url. All of these generate the same route and helpers:

    # technically, you don't need :as option here, because it's the same as path
    get "to-do", to: "current_to_do_items#landing"
    
    get "to-do", to: "current_to_do_items#landing", as: :to_do
    get "to-do", to: "current_to_do_items#landing", as: "to-do"
    

    https://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Base.html#method-i-match


    You can quote symbols if you have characters other than letters and underscores:

    { "to-do": :"to do!" }