ruby-on-rails – Rails 3 |路由:现在如何重命名资源标题?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails 3 |路由:现在如何重命名资源标题?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
通常我有MenuItems模型,并尝试使’/ menu_items(/:id(:/ some_action))’的URL看起来像’/ menu(/:id(:/ some_action))’

在Rails 2.3.5它是

map.resources:menu_items,as as => :菜单,:path_names => {:new => ‘add’}

现在在Rails 3.0.3中,我可以使用这段巨大的代码段来处理它

resources :menu_items,:path_names => { :new => 'add' }
  match 'menu/' => 'menu_items#index',:as => :menu
  match 'menu/add' => 'menu_items#new',:as => :new_menu
  match 'menu/:id' => 'menu_items#show',:as => :show_menu
  match 'menu/:id/edit' => 'menu_items#edit',:as => :edit_menu

但是由于代码量庞大,因此看起来不正确.
好像就像第二个Rails的map.some_name一样.

任何帮助/建议/指南? (谢谢)

解决方法

http://guides.rubyonrails.org/routing.html#customizing-resourceful-routes
resources :menu,:controller => "menu_items",:path_names => { :new => "add" }

输出与你以后的输出非常接近:

menu_index GET    /menu(.:format)             {:controller=>"menu_items",:action=>"index"}
           POST   /menu(.:format)             {:controller=>"menu_items",:action=>"create"}
  new_menu GET    /menu/add(.:format)         {:controller=>"menu_items",:action=>"new"}
 edit_menu GET    /menu/:id/edit(.:format)    {:controller=>"menu_items",:action=>"edit"}
      menu GET    /menu/:id(.:format)         {:controller=>"menu_items",:action=>"show"}
           PUT    /menu/:id(.:format)         {:controller=>"menu_items",:action=>"update"}
           DELETE /menu/:id(.:format)         {:controller=>"menu_items",:action=>"destroy"}
原文链接:https://www.f2er.com/ruby/272505.html

猜你在找的Ruby相关文章