2012-01-18 70 views
1

在我的路线文件的URL我也行:Rails的LINK_TO产生与子目录

match 'documents/:category/:id' => 'documents#show' 

允许我使用的网址,如:

*本地主机:3000 /文件/ lesson_plans/day_01 *

该URL可以正常工作,但我无法弄清楚如何使用link_to生成它。


link_to 'day_01', document_path('/lesson_plans/day_01') 

返回错误:

No route matches {:action=>"show", :controller=>"documents", :id=>"/lesson_plans/day_01"}


link_to 'day_01', document_path(:category => 'lesson_plans', :id => 'day_01') 

的作品,但它产生的网址:

localhost:3000/documents/day_01?category=lesson_plans

这是不干净的enoug H。


有没有一种方法来生成的网址:

localhost:3000/documents/lesson_plans/day_01

回答

1

试试这个:

match 'documents/:category/:id' => 'documents#show', :as => :document 

= link_to 'day_01', document_path('day_01', :category => 'lesson_plans') 

它应该生成:

http://localhost:3000/documents/lesson_plans/day_01 
1
<%= link_to "day_01", {:controller => :documents, :action => :show, :category => "lesson_plans", :id => "day_01"} %>