Routing Resources Doesn't Set up Routing Properly - ruby-on-rails

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

Related

Rails custom routes

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

Rails how to make example.com/post/1 to example.com/blog/post/1

I have a blog with root
root 'posts#index'
And works best with example.com/ to example.com/posts
But what I want is something like this:
example.com/blog/posts/1.
I've tried creating blog Controller and add
resources :blog do
resources :posts
end
But this is making my routes to blog/:id/posts/:id
If you don't have the relationship between the post and the blog as you mentioned, rails gives you the freedom to declare routes as our own.
so, to make the route example.com/posts/1 to, example.com/blog/posts/1, just add a custom route at the last.
get '/blog/posts/:id', to: :show, controller: 'posts'
what this does is over rides the previous route and make this route final.
Now type rake routes and it will give the last route for you as,
GET /blog/posts/:id(.:format) posts#show
Now you can access using,
example.com/blog/posts/1
Reference for rails routing
Just to expand upon #Sravan answer. If you have multiple routes that will start with /blog/ you might want to check Rails guide on routing.
You can add something along the lines of
scope '/blog' do
resources :posts
resources :users
resources :images
end
Which will create corresponding routes under /blog/.
namespace :blog do
resources :posts
resources :users
resources :images
end
And your controller with namespace will look like this: Blog::PostsController

Missing helper path when doing rake routes

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

Custom actions in rails controller with restful actions?

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.

Cleaner way to add routes to already defined resources :users

My routes.rb has:
resources :users
Now if I add more routes in my users_controller, is there a way to bunch them together somehow instead of having a seperate line for each new route I add?
resources :users
# routes for signin, signout, password_recovery that are in users_controller
This is rails 3.
You can add routes that act on the collection or individual members with the member and collection block.
See the "Adding More RESTful Actions" section of the Rail's routing guide.

Resources