我有以下问题,我不明白为什么它不工作:
我有路由:
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_]+/
一切顺利
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_]+/}
但是当我试图建立链接
= 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]
你知道我做错了什么吗?
我可能会尝试不同的策略。
get 'tickets/to/:id' => 'landings#show_direct', as: 'tickets_to'
get 'tickets/from/:id' => 'landings#show_reverse', as: 'tickets_from'
:
link_to "Tickets to Moscow!", tickets_to_url('Moscow')
link_to "Tickets from Moscow!", tickets_from_url('Moscow')
谢谢大家!
这是我的错
你应该使用
get "/:id", to: 'landings#show_direct', as: :direct_landing, id: /ticket-from-[a-zA-Z0-9_]+/
而不是get "/:id" => 'landings#show_direct', as: :direct_landing, id: /ticket-from-[a-zA-Z0-9_]+/
现在下面的helper工作正常
direct_landing_url(:id => 'ticket-to-moscow')