0

我有以下问题,我不明白为什么它不起作用:

我有路线:

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

耙路线 | grep 登陆

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]

任何想法我做错了什么?

4

2 回答 2

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')
于 2013-06-09T17:30:23.773 回答
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')
于 2013-08-07T05:42:07.047 回答