I have created an extra route called 'add' in my Groups controller.
get 'groups/add', to: 'groups#add'
My goal is to have the following link http://localhost:3000/groups/4/add
I thought about doing this nested route
resources :groups do
get 'groups/add', to: 'groups#add'
end
However when I run rake routes it is not listed. I have never really worked much with 'custom' routes before. I typically make due with the ones generated by a scaffold. How can I achieve this?
The following nested route has fixed my issue:
resources :groups do
get 'add', on: :member
end
Related
I am creating a custom route like:
namespace :admin do
root 'users#index'
resources :users do
get 'admin_login' => 'users#admin_login'
end
end
But when I see with rake routes:
admin_user_admin_login GET /admin/users/:user_id/admin_login(.:format) admin/users#admin_login
Why :user_id is added here?
I just want it without :user_id.
Because you are creating a custom route within the users resource. Rails is doing exactly what you are telling it to do. You would like to show the "admin_login" route for a specified user (that's what you're currently telling rails to do).
Move the:
get 'admin_login' => 'users#admin_login'
Line of code outside of the resources block and you'll be able to create your custom route.
You need to specify an on option to tell Rails that it works on a collection and not a member resource. According to the official Rails routing guide
You can leave out the :on option, this will create the same member
route except that the resource id value will be available in
params[:photo_id] instead of params[:id].
You can also remove the => 'users#admin_login' part as that is the default behavior.
So the solution to your problem is to add on: :collection or place it inside a block like
namespace :admin do
root 'users#index'
resources :users do
collection do
get 'admin_login'
end
end
end
I wanted to do like http://domain:3000/users/id/edit/details. I've already create a new action under app>views>users>details.html.erb. Also in UsersController i put:
def details
....
end
when I run rake routes, I got missing Helper Path/Url on the left side for GET, /users/:id/edit/details(.:format), Users#Details.
Also in routes.rb, I've:
resources :users
I try to insert:
match "users/:id/edit/details", to: 'users#details', via: 'get'
but stil didnt solve my problem. Please help me!
In routes, you'd want to do this:
resources :users do
get :details
end
In this, you'd have the advance form. The url for this will be
/users/:id/details/
Check rake routes to see what's in there.
You may need another update method (the form post action) to handle the advanced form
What you are looking for is called nested resources
resources :users do
resources :details
end
Take a look at http://guides.rubyonrails.org/routing.html#nested-resources for more info
Normally I would expect resources to set the routes where
/groups/:id
However, when I call rake routes I get
GET /groups(.:format) groups#show
Also, whenever I use the view helper method groups_path(group.id) it provides the url link as
/groups.1
Finally in my routing file
resource :groups
This is the case for edit, update, and destroy actions as well. Finally, it doesn't produce an index action. I have to create that after I write the resources method. What's wrong and how do I fix this?
Pluralize that resource:
resources :groups
resources :groups
instead of
resource :groups
I'm new with RoR so this is a newbie question:
if I have a controller users_controller.rb and I add a method foo, shouldn't it create this route?
http://www.localhost:3000/users/foo
because when I did that, I got this error:
Couldn't find User with id=foo
I of course added a view foo.html.erb
EDIT:
I added to routes.rb this code but I get the same error:
resources :users do
get "signup"
end
This doesn't work automatically in rails 3. You'll need to add
resource :users do
get "foo"
end
to your routes.rb
You'll definitely want to have a look at http://guides.rubyonrails.org/routing.html, it explains routing pretty well.
Rails is directing you to the show controller and thinks that you're providing foo as :id param to the show action.
You need to set a route that will be dispatched prior to being matched as /users/:id in users#show
You can accomplish this by modifying config/routes.rb by adding the following to replace your existing resource describing :users
resource :users do
get "foo"
end
Just to add to the other answers, in earlier versions of Rails there used to be a default route
match ':controller(/:action(/:id))(.:format)'
which gave the behaviour you describe where a request of the form controller/action would call the given method on the given controller. This line is still in routes.rb but is commented out by default. You can uncomment it to enable this behaviour but the comment above it explains why this is not recommended:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
At the schema ':controller/:action(.:format)', you can also easily do the following
resources :users do
get "foo", on: :collection
end
or
resources :users do
collection do
get 'foo'
end
end
http://guides.rubyonrails.org/routing.html#adding-collection-routes
I'm having some trouble with using creating my own actions inside a controller I generated using the scaffold.
I understand everything maps to the restful actions but I'm building a user controller where users can login/logout, etc but when I created the action and declared it in the routes.rb I get this error when I visit users/login
Couldn't find User with id=login
It tries to use login as a ID parameter instead of using it as an action.
Routes.rb
match 'users/login' => 'users#login'
I think I'm doing something wrong with the routes so if anybody could help me that would be great.
Thanks
I assume your routes.rb looks like this:
resources :users
match 'users/login' => 'users#login'
The problem is that Rails uses the first route that matches. From the documentation:
Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action’s route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.
So either define your custom route before resources :users:
match 'users/login' => 'users#login'
resources :users
…or use this syntax for adding more RESTful actions:
resources :users do
collection do
match 'login'
end
end
To see the existing routes (and their order) run rake routes from the command line.