我有以下问题,我无法理解为什么它不起作用:
我有路线:
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')
我有错误消息
No route matches {:action=>"show_reverse", :controller=>"landings", :id=>"ticket-to-moscow", :format=>nil} missing required keys: [:id]
任何想法我做错了什么?
答案 0 :(得分:2)
我可能会尝试不同的策略。
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')
答案 1 :(得分:0)
谢谢大家!
这是我的错误
您应该使用
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_]+/
现在关注帮助工作正常
direct_landing_url(:id => 'ticket-to-moscow')