Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 | Routing : How to rename resource title now?

Generally I have MenuItems model and trying to make '/menu_items(/:id(:/some_action))' URLs looks like '/menu(/:id(:/some_action))'

In Rails 2.3.5 it was

map.resources :menu_items, :as => :menu, :path_names => { :new => 'add' }

Now in Rails 3.0.3 I'm able to handle it using this huge paragraph of code

  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

But it looks incorrect because of huge amount of code. Seems :as works like 2nd Rails' map.some_name now.

Any help/suggestions/guides? (Thanks)

like image 568
jibiel Avatar asked Jan 01 '11 15:01

jibiel


2 Answers

http://guides.rubyonrails.org/routing.html#customizing-resourceful-routes

resources :menu, :controller => "menu_items", :path_names => { :new => "add" }

Output is quite close to what you're after:

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"}
like image 141
Heikki Avatar answered Nov 15 '22 13:11

Heikki


Another way

resources :menu, :as => :menu_items, :controller => :menu_items

This approach has the advantage that the exiting helper methods such as menu_items_path still work.

like image 36
timeon Avatar answered Nov 15 '22 14:11

timeon