Ruby on rails 自定义每个操作的资源路由
我有一个Ruby on rails 自定义每个操作的资源路由,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,routes,custom-routes,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,Routes,Custom Routes,我有一个参考资料:posts。如何将其路径自定义为以下路径,以及如何使用普通资源路径名调用它: URL Controller action Helper function 'q' 'posts#index' posts_path 'q/(:id)' 'posts#show' post_path(:id) 'ask' 'posts#new' new_post_path 'q
参考资料:posts
。如何将其路径自定义为以下路径,以及如何使用普通资源路径名调用它:
URL Controller action Helper function
'q' 'posts#index' posts_path
'q/(:id)' 'posts#show' post_path(:id)
'ask' 'posts#new' new_post_path
'q' 'posts#create' posts_path
这是我尝试过的,但没有达到上面的预期效果
get 'q' => 'posts#index', as: :posts
get 'q/(:id)' => "posts#show", as: :post
get 'ask' => 'posts#new'
您可能会收到一个错误,因为您试图分配一个已在使用的路由名称
resources posts
调用将生成posts
和post
路由的定义。如果将as:..
子句更改为引用不同的(未使用的)名称,则不会再出现该错误。请尝试参考资料:posts path=>q'