Michaël
Michaël

Reputation: 1130

Rails 3: Problem with a route to new action

I have a problem with rails 3 routes.

I want add a new action called "gestion_etudiant" with a new view "gestion_etudiant.html.erb".

I have on my index page a link like this

<%= link_to "Administration", {:controller => "users", :action => "gestion_etudiant"} %>

I also try this:

<%= link_to "Administration", "/users/gestion_etudiant" %>

In my controller:

def gestion_etudiant
  @users = User.find(:all)
end

but when I clic on the link, I always have this error:

ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=gestion_etudiant

I have this in my routes file:

resources :users

And I've also try to add:

match "users/gestion_etudiant", :to => "users#gestion_etudiant"

and

resources :users, :only => [:gestion_etudiant]

But I can not access my page "gestion_etudiant.html.erb". Can anybody suggest why?

Upvotes: 1

Views: 400

Answers (2)

philnash
philnash

Reputation: 73075

In your routes, try:

resources :users do
  collection do
    get 'gestion_etudiant'
  end
end

You can check the routes you have in your application by running rake routes

Upvotes: 1

Ant
Ant

Reputation: 3887

Try this:

# router:

resources :users do
  get :gestion_etudiant, :on => :collection
end

# view:

link_to "Administration", gestion_etudiant_users_path

# Controller

def gestion_etudiant
  @users = User.all # Don't use find with :all as it will be deprecated in rails 3.1
end

Upvotes: 2

Related Questions