Ruby on rails 使用URL中的连字符连接Map.connect
我想要Ruby on rails 使用URL中的连字符连接Map.connect,ruby-on-rails,Ruby On Rails,我想要http://localhost:3000/note-1828映射到控制器操作。我试过这个: map.connect "note-:id", :controller => "annotations", :action => "show", :requirements => { :id => /\d+/ } 但是它似乎不起作用(没有路由匹配“/note-1828”与{:method=>:get})。我应该怎么做呢?路由变量(如:id)只能
http://localhost:3000/note-1828
映射到控制器操作。我试过这个:
map.connect "note-:id",
:controller => "annotations",
:action => "show",
:requirements => { :id => /\d+/ }
但是它似乎不起作用(没有路由匹配“/note-1828”与{:method=>:get}
)。我应该怎么做呢?路由变量(如:id
)只能发生在路径分隔符之间,在本例中是斜杠
您的最佳选择是暂停您的路线,改为使用/notes/:id
但是,很可能,您正在重写一个现有站点,并希望保留您的URL。在这种情况下,我会使用.htaccess
和mod_rewrite
如下方式重新路由:
RewriteRule note-(\d+) /notes/$1 [R=301]
(显然,.htaccess
必须位于/public
目录中)路由变量(如:id
)只能在路径分隔符之间发生,在这种情况下是斜杠
您的最佳选择是暂停您的路线,改为使用/notes/:id
但是,很可能,您正在重写一个现有站点,并希望保留您的URL。在这种情况下,我会使用.htaccess
和mod_rewrite
如下方式重新路由:
RewriteRule note-(\d+) /notes/$1 [R=301]
(显然,
.htaccess
必须在/public
目录中)同意Leonid的观点。如果您只想创建一些“漂亮”的URL,可以将to_param方法应用于notes模型。因此:
def to_param
"#{id}-notes"
end
并添加一个RESTful路由:
map.resources :annotations, :as => "notes"
会给你这样的东西:
http://yourdomain.com/annotations/1828-notes
如果您不想使用“notes”部分,可以将路线映射到
map.connect "/:id", :controller=>"annotations", :action=>"show"
得到
http://yourdomain.com/1828-notes
同意莱昂尼德的观点。如果您只想创建一些“漂亮”的URL,可以将to_param方法应用于notes模型。因此:
def to_param
"#{id}-notes"
end
并添加一个RESTful路由:
map.resources :annotations, :as => "notes"
会给你这样的东西:
http://yourdomain.com/annotations/1828-notes
如果您不想使用“notes”部分,可以将路线映射到
map.connect "/:id", :controller=>"annotations", :action=>"show"
得到
http://yourdomain.com/1828-notes