SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast jitao's Avatar
    Join Date
    Feb 2003
    Location
    Shanghai
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails question about route

    hello, When I'm learning the rails route system I find this problem.

    First, I have all my controllers under the app folder, and I decide to move some of them to a admin under app so change the routes.rb like following
    Code Ruby:
    map.with_options(:namespace=>"admin") do |admin|
        admin.resources :instruments, :groups, :markets
    end
    now I can point my browser to http://localhost:3000/admin/groups and view a list of groups as usual.

    But when I point my mouse to those links (new, show, edit, destroy), I find a problem those generated links still pointed to the old location http://localhost:3000/groups

    I use new_group_url method to generate links to create a new group.
    Code Ruby:
    <%= link_to "New Group", new_group_url %>

    My question is what is the method that generate proper link url to my nested controllers (http://localhost:3000/admin/groups)?

    Thanks you

  2. #2
    SitePoint Enthusiast jitao's Avatar
    Join Date
    Feb 2003
    Location
    Shanghai
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    After read the rails guide and other tutorials now I changed my route setting to following:
    Code Ruby:
    ActionController::Routing::Routes.draw do |map|
      map.namespace :admin do |admin|
        admin.resources :groups, :markets
      end
    Now when I run rake routes at the command line it generates a routes table:
    Code:
             admin_groups GET    /admin/groups(.:format)               {:controller=>"admin/groups", :action=>"index"}
                          POST   /admin/groups(.:format)               {:controller=>"admin/groups", :action=>"create"}
          new_admin_group GET    /admin/groups/new(.:format)           {:controller=>"admin/groups", :action=>"new"}
         edit_admin_group GET    /admin/groups/:id/edit(.:format)      {:controller=>"admin/groups", :action=>"edit"}
              admin_group GET    /admin/groups/:id(.:format)           {:controller=>"admin/groups", :action=>"show"}
                          PUT    /admin/groups/:id(.:format)           {:controller=>"admin/groups", :action=>"update"}
                          DELETE /admin/groups/:id(.:format)           {:controller=>"admin/groups", :action=>"destroy"}
            admin_markets GET    /admin/markets(.:format)              {:controller=>"admin/markets", :action=>"index"}
                          POST   /admin/markets(.:format)              {:controller=>"admin/markets", :action=>"create"}
         new_admin_market GET    /admin/markets/new(.:format)          {:controller=>"admin/markets", :action=>"new"}
        edit_admin_market GET    /admin/markets/:id/edit(.:format)     {:controller=>"admin/markets", :action=>"edit"}
             admin_market GET    /admin/markets/:id(.:format)          {:controller=>"admin/markets", :action=>"show"}
                          PUT    /admin/markets/:id(.:format)          {:controller=>"admin/markets", :action=>"update"}
                          DELETE /admin/markets/:id(.:format)          {:controller=>"admin/markets", :action=>"destroy"}
                                 /:controller/:action/:id              
                                 /:controller/:action/:id(.:format)
    From this table it looks like now rails know the proper way to interpret and generate help method BUT when I use
    Code Ruby:
    <&#37;= link_to "Show", admin_group %>
    in my view file I still have an error message saying: undefined local variable or method `admin_group' for #<ActionView::Base:0xb5ddc308>

    Does anyone here met same problem? my rails version is 2.3.5? What could be the solution?
    Thank you!!


  3. #3
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The name of the route is admin_group, that means the helper methods to get the path are:
    admin_group_path
    and
    admin_group_url
    You need to use one of those in your link_to.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •