Rails custom and default routes - ruby-on-rails

I'm trying to define custom routes to my controller and I need to use some of the default routes too. Is there any simple solution?
So far I've something like this
resources :users do
member do
get 'users/:id', to: 'users#show'
delete 'users/:id', to: 'users#destroy'
end
collection do
post 'users', to: 'users#create'
post 'users/login', to: 'users#login'
end
end
resources :users, :only => [:show, :destroy, :create, :login]
I don't need nor want the index route but with this settings it's still trying to route GET users/ to user_controller index method.
I know that there is probably some simple and obvious answer but I'm not able to find it.
Thank's in advance.

You got your routes wrong. The resources :users generates seven default routes which include the index route as well. You need to tweak the code to below
resources :users, :only => [:show, :destroy, :create] do
collection do
post 'login', to: 'users#login'
end
end
Note:
If you noticed, I've removed the custom routes for show,create and delete as they are generated by default.

Your first line defines the route to the index action. Define a resource once only. Read the routing guide.
resources :users, :except => [:index] do
collection do
post 'users/login', to: 'users#login'
end
end
Run rake routes from the command line in your project root folder to see all your route definitions.

Related

Ruby on Rails: rosource RESOURCE_NAME dosent generate index route/action

im currently trying to use the resource but one problem im having , that when i do the following
resource :orders
the route /orders dosent route to OrdersController#index rather it points to the show action of the controller, how can i fix this issue ?
becuase of this problem im having to do this which i feel is kinda hack and not good
get '/orders', to: 'orders#index'
get '/orders/:id', to: 'orders#show'
this is my routes.rb file
Rails.application.routes.draw do
get '/carts', to: 'carts#index'
get '/payments', to: 'payments#index'
post '/payments', to: 'payments#add_credits'
get '/orders', to: 'orders#index'
get '/orders/:id', to: 'orders#show'
resources :users do
resource :orders, only: %i[show create index]
resource :carts, only: %i[create destroy], path: 'cart', as: 'cart'
end
resource :sessions, only: [] do
post 'login', action: :create
post 'logout', action: :destroy
get 'login', action: :new
end
resources :products
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
thanks for your answer :)
Don't use
resource :orders
use
resources :orders
You would only use resource when the item orders is a single entity in your application... which is to say you're using the plural to refer to that one item.
Move the resource for orders outside of the Users routes.
Just and FYI, you can have both the full resource outside of Users and then those restricted routes inside Users, but I'm not sure what the goal is here so it is up to you to decide that.
Rails.application.routes.draw do
get '/carts', to: 'carts#index'
get '/payments', to: 'payments#index'
post '/payments', to: 'payments#add_credits'
resources :orders
resources :users do
resource :orders, only: %i[show create index] <-- not sure if this remains here
resource :carts, only: %i[create destroy], path: 'cart', as: 'cart'
end
...

Does the order of the routes effect which controller is accessed?

In my app I have grants and I want the url to be root/grant_id instead of root/grants/grant_id. I have this in my routes
Rails.application.routes.draw do
...
root 'static_pages#welcome'
# get 'home' => 'static_pages#home'
get 'about' => 'static_pages#about'
get 'faq' => 'static_pages#faq'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
get 'dashboard' => 'dashboard#index'
resources :users do
resources :projects
member do
get 'access_granted'
put 'access_granted'
get 'remove_access'
put 'remove_access'
end
end
resources :profiles
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resource :request_access, only: [:show, :new, :create]
resources :grants, :path => '' do
resources :app_types do
resources :submissions
end
end
get 'grants' => 'grants#index'
resources :matches
end
When I put resources :matches below the resources :grants, :path => '' do line I get the error "Couldn't find Grant" and I see that request parameters are
{"controller"=>"grants", "action"=>"show", "id"=>"matches"}. When I put resources :matches above the the grant line everything works fine. Its almost like something in the grant route isn't closing and is forcing any lines below it to look for the grant controller. A simple solution is just keeping everything above that line but I'm trying to understand why this is happening.
I also noticed that even though I define the grant#index as grants, when I rake routes I see:
grants GET / grants#index
GET /grants(.:format) grants#index
So two questions
1. Is :path => '' the correct way to remove the grants/ part of the url.
2. Why is everything below the grants route getting sent to the grants controller?
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, the problem you're having is that matching grants to "" means your grants INDEX route is /, and your grants SHOW route is /:grant_id, which will match any route. If you want to have this kind of route (which I would advise against), it has got to be at the bottom of the routes file.
You can read more about routing here: http://guides.rubyonrails.org/routing.html

Rails is there a way to collect all routes

I am using this as an example:
https://github.com/toshimaru/Rails-4-Twitter-Clone
The route file looks like:
Rails.application.routes.draw do
root 'static_pages#home'
resources :users do
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :tweets, only: [:index, :create, :destroy]
resources :relationships, only: [:create, :destroy]
get 'signup' => 'users#new'
get 'signin' => 'sessions#new'
delete 'signout' => 'sessions#destroy'
get 'about' => 'static_pages#about'
match '*path' => 'application#routing_error', via: :all
end
It appears that in some controllers there are comments mentioning which route it uses, however it's not specified in the route file. For instance:
# GET /users
def index
#users = User.all
end
# GET /users/1
def show
#tweet = current_user.tweets.build if signed_in?
#feed_items = #user.tweets.paginate(page: params[:page])
end
(in users_controller.rb)
My question is, how does rails app know that there is an endpoint here? I would like to know whether I can actually collect all the routes in one file?
What I am intending to do is, I would like to replace current routes.rb file with all routes.
Its specified in route file
When, we write
resources :users
It creates CRUD routes for specified resource namely index, new, create, show, edit, update, destroy routes for resource, here in this case resource is user.
You can see all routes for user by
rake routes | grep 'users'
and to list all routes in application
rake routes
resources :users has automatically created all RESTful routes for you, although you can limit it to only certain routes (as your example does with sessions, tweets, and relationships).
You can see all the routes from the console... in the root of your project do rake routes
See here for an explanation...
http://guides.rubyonrails.org/routing.html#listing-existing-routes
You can access the output of rake routes in your application....
output = `rake routes`
(note the use of backticks in the above)

Aliasing routes in rails 4

If I have the following routes
resources :pages do
resources :sections
end
I get routes that look like this:
/pages = #index
/pages/:id = #show
/pages/:id/edit = #edit
...etc
How can I go about making it so that the url for the #show action of the pages controller looks like '/:id', without the '/pages/' prefix? should I exclude #show from resources :page & create a get route + alias for it separately? or is there a way to do it from inside the resources :page block? Thanks in advanced.
EDIT:
Changed it to:
resources :pages, except: [:show] do
resources :sections
end
get '/:id', to: 'pages#show'
& rerouting non-existing :ids' to 404 for now, let me know if there's a better solution. Thanks.
get '/:id', to: 'pages#show', as: 'page'
Make sure this is at the bottom of your routes.rb file, otherwise it is going to hijack requests to other routes.
This also gives you page_url and page_path helper methods. But to use them you must exclude show action from previous routes.
resources :pages, except: [:show]

Abstracting rails route

I want to replace the normal /users/:id route that is created by the resources command, with a more abstract /profile route. It won't be possible to view other users profiles in my app, and therefor the current route is unnecessary specific.
I have tried to overwrite the route created by 'resources :users' with:
get '/profile', to: 'users#show'
and other variances and combinations, but can't seem to get it right. Either the controller can't find the user because of a missing id or it simply can't find the route.
Thanks for the help!
You can use this code in routes.rb file:
resources :users, :except => :show
collection do
get 'profile', :action => 'show'
end
end
It will generate url "/users/profile".
But, if u want to use only '/profile', then don't create route as collection inside users resources block.
resources :users, :except => :show
get 'profile' => "users#show", :as => :user_profile
It will redirect '/profile' to show action in users controller.
I suggest simply adding a users/me route pointing to the show action of your UsersController like so:
resources :users, only: [] do
collection do
get 'me', action: :show
end
end
You can also use the match keyword in routes.rb file.
match 'users/:id' => 'users#show', as: :user_profile, via: :get

Resources