GrégoireC
GrégoireC

Reputation: 279

link_to redirect to a wrong controller

I have a layout for my admin area called 'layout_admin' . In 'layout_admin', I have :

<li><%= link_to 'Contenu', :action=>'index', :controller=>'contents' %></li>
<li><%= link_to 'Petitions', :controller => 'petitions', :action => 'index' %></li>

The first link for Contenu works fine, but the second one (for petitions) drives me to a strange error :

Routing Error No route matches {:controller=>"admin/edito"}

In the address bar I have : localhost:3000/admin/petitions

In routes.rb I have :

namespace :admin do resources :petitions 
end

I must also precise that "edito" is another controller outside the admin area which has one action "index". In routes.rb I have get "edito/index" concerning edito_controller.

Does somebody have an idea of the source of the problem ? Thansk.

Full Rake routes :

        temoignages GET    /temoignages(.:format)              temoignages#index

                POST   /temoignages(.:format)              temoignages#create
 new_temoignage GET    /temoignages/new(.:format)          temoignages#new
edit_temoignage GET    /temoignages/:id/edit(.:format)     temoignages#edit
     temoignage GET    /temoignages/:id(.:format)          temoignages#show
                PUT    /temoignages/:id(.:format)          temoignages#update
                DELETE /temoignages/:id(.:format)          temoignages#destroy
admin_petitions GET    /admin/petitions(.:format)          admin/petitions#index
                POST   /admin/petitions(.:format)          admin/petitions#create
new_admin_petition GET    /admin/petitions/new(.:format)      admin/petitions#new
edit_admin_petition GET    /admin/petitions/:id/edit(.:format) admin/petitions#edit
 admin_petition GET    /admin/petitions/:id(.:format)      admin/petitions#show
                PUT    /admin/petitions/:id(.:format)      admin/petitions#update
                DELETE /admin/petitions/:id(.:format)      admin/petitions#destroy
 admin_contents GET    /admin/contents(.:format)           admin/contents#index
                POST   /admin/contents(.:format)           admin/contents#create
new_admin_content GET    /admin/contents/new(.:format)       admin/contents#new
edit_admin_content GET    /admin/contents/:id/edit(.:format)  admin/contents#edit
  admin_content GET    /admin/contents/:id(.:format)       admin/contents#show
                PUT    /admin/contents/:id(.:format)       admin/contents#update
                DELETE /admin/contents/:id(.:format)       admin/contents#destroy
    admin_posts GET    /admin/posts(.:format)              admin/posts#index

                POST   /admin/posts(.:format)              admin/posts#create
  new_admin_post GET    /admin/posts/new(.:format)          admin/posts#new
  edit_admin_post GET    /admin/posts/:id/edit(.:format)     admin/posts#edit
     admin_post GET    /admin/posts/:id(.:format)          admin/posts#show
                PUT    /admin/posts/:id(.:format)          admin/posts#update
                DELETE /admin/posts/:id(.:format)          admin/posts#destroy
                GET    /admin/posts(.:format)              admin/posts#index

                POST   /admin/posts(.:format)              admin/posts#create
                GET    /admin/posts/new(.:format)          admin/posts#new
                GET    /admin/posts/:id/edit(.:format)     admin/posts#edit
                GET    /admin/posts/:id(.:format)          admin/posts#show
                PUT    /admin/posts/:id(.:format)          admin/posts#update
                DELETE /admin/posts/:id(.:format)          admin/posts#destroy
  admin_backend_index GET    /admin/backend(.:format)            admin/backend#index
                POST   /admin/backend(.:format)            admin/backend#create
  new_admin_backend GET    /admin/backend/new(.:format)        admin/backend#new

  edit_admin_backend GET    /admin/backend/:id/edit(.:format)   admin/backend#edit
  admin_backend GET    /admin/backend/:id(.:format)        admin/backend#show
                PUT    /admin/backend/:id(.:format)        admin/backend#update
                DELETE /admin/backend/:id(.:format)        admin/backend#destroy
     lois_index GET    /lois/index(.:format)               lois#index
      lois_show GET    /lois/show(.:format)                lois#show
    edito_index GET    /edito/index(.:format)              edito#index
  reponses_index GET    /reponses/index(.:format)           reponses#index
  reponses_show GET    /reponses/show(.:format)            reponses#show
  lettres_index GET    /lettres/index(.:format)            lettres#index
   lettres_show GET    /lettres/show(.:format)             lettres#show
       accueils POST   /accueils(.:format)                 accueils#create
   new_accueils GET    /accueils/new(.:format)             accueils#new
  edit_accueils GET    /accueils/edit(.:format)            accueils#edit
                GET    /accueils(.:format)                 accueils#show
                PUT    /accueils(.:format)                 accueils#update
                DELETE /accueils(.:format)                 accueils#destroy
           root        /                                   accueil#index

Upvotes: 0

Views: 985

Answers (1)

Chris Peters
Chris Peters

Reputation: 18090

See the route names in the left column of your rake routes? Use that info to build calls to different path helpers:

<li><%= link_to 'Contenu', admin_contents_path %></li>
<li><%= link_to 'Petitions', admin_petitions_path %></li>

In general, that's how you should be building URLs in your Rails 3+ app.

Read through Rails Routing from the Outside In for more information (particularly 2.3: Paths and URLs).

Update:

You should look into using these _path-style helpers on the view that's being pulled up when you visit /admin/petitions.

I bet you have a link on the page similar to this:

<%= link_to "Link Text", :controller => "edito", :action => "index" %>

It's trying to find edito in the admin namespace because that's where you are within the app when you visit /admin/petitions.

To fix, you need to update it to read like this:

<%= link_to "Link Text", edito_index_path %>

Wash, rinse, and repeat for all links, forms, and url_for references in your app.

Upvotes: 1

Related Questions