سؤال

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?

هل كانت مفيدة؟

المحلول 2

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')

نصائح أخرى

I'd probably try a different strategy.

get 'tickets/to/:id' => 'landings#show_direct', as: 'tickets_to'
get 'tickets/from/:id' => 'landings#show_reverse', as: 'tickets_from'

Then:

link_to "Tickets to Moscow!", tickets_to_url('Moscow')
link_to "Tickets from Moscow!", tickets_from_url('Moscow')
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top