ruby-on-railsrouteslink-tonamed-routing

Rails, named route with regexp doesn't work


I have following problem and I can't understand why it's doesn't work:

I have routes:

get "/:id" => 'landings#show_direct',  as: :direct_landing,  id: /ticket-from-[a-zA-Z0-9_]+/
get "/:id" => 'landings#show_reverse', as: :reverse_landing, id: /ticket-to-[a-zA-Z0-9_]+/

And it's work fine

localhost:3000/ticket-form-moscow
localhost:3000/ticket-to-moscow

rake routes | grep landing

direct_landing  GET /:id(.:format) landings#show_direct {:id=>/ticket-from-[a-zA-Z0-9_]+/}
reverse_landing GET /:id(.:format) landings#show_reverse {:id=>/ticket-to-[a-zA-Z0-9_]+/}

But when I try to build link

= link_to @landing.title_to, reverse_landing_url('ticket-to-moscow')

I have error message

No route matches {:action=>"show_reverse", :controller=>"landings", :id=>"ticket-to-moscow", :format=>nil} missing required keys: [:id]

Any ideas what I do wrong?


Solution

  • Thanks for all!

    it was my mistake

    You should to use

    get "/:id", to: 'landings#show_direct',  as: :direct_landing,  id: /ticket-from-[a-zA-Z0-9_]+/
    

    instead

    get "/:id" => 'landings#show_direct',  as: :direct_landing,  id: /ticket-from-[a-zA-Z0-9_]+/
    

    Now following helper works fine

    direct_landing_url(:id => 'ticket-to-moscow')