How to rename index path in routes file? - ruby-on-rails

How to rename index path only?
routes.rb
resources :tasks, :except => [:create] do
collection do
..............
end
member do
.............
end
end
instead of /tasks in URL I need /trigger or even /tasks/trigger will do, im on rails3.
I tried
1.
collection do
'/', to: 'tasks#trigger'
end
and
2.
resources :tasks, :except => [:create] do
get '/task', to: 'task#trigger', as: task_index
end
both throw up errors.
any ideas?

For URL like '/trigger' add to routes.rb:
get 'trigger' => 'tasks#index'
For URL like '/tasks/trigger' modify routes.rb:
resources :tasks, :except => [:create] do
collection do
get 'trigger' => 'tasks#index'
end
end
If your index method in task's controller named as 'trigger, 'tasks#index' is replaced by 'tasks#trigger'

If you want to make your own index route then you need to do two things
1- you need to disable index path created by resources
2- Define your own path whatever you want.
As per your requirement you want to make index route as '/trigger'.
Following is the solution.
get 'trigger' => 'tasks#index'
resources :tasks, :except => [:create, :index] do
collection do
..............
end
member do
.............
end
end

Just restraint index route from being generated in your resources call:
resources :tasks, :except => [:create, :index]
And then do your own custom route. For '/trigger':
get 'trigger', to: 'task#trigger', as: 'trigger

Related

Rails - Deploying to Heroku with duplicate routes

I'm deploying my Rails app that uses the clearance gem to Heroku. Everything works fine in development but I'm running into trouble with the gem generated routes I get.
When attempting to deploy to Heroku, I get the error...
ArgumentError: Invalid route name, already in use: 'sign_in'
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
remote: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
I'm not seeing where to restrict the duplicates or where they would be generated with any of my resources:
Please see routes.rb file below
Routes.rb
Rails.application.routes.draw do
resources :passwords, controller: "clearance/passwords", only: [:create, :new]
resource :session, controller: "clearance/sessions", only: [:create]
resources :users, controller: "clearance/users", only: [:create] do
resource :password,
controller: "clearance/passwords",
only: [:create, :edit, :update]
end
get "/sign_in" => "clearance/sessions#new", as: "sign_in"
delete "/sign_out" => "clearance/sessions#destroy", as: "sign_out"
get "/sign_up" => "clearance/users#new", as: "sign_up"
get 'newSignUp', to: 'signups#new'
post 'newSignUp', to: 'signups#create'
get 'newTrip', to: 'trips#new'
post 'newTrip', to: 'trips#create'
get 'trips/:id/send_itinerary' => 'trips#send_itinerary', as: :trips_send_itinerary
root 'static_pages#home'
get 'static_pages/home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
resources :signups
resources :tripitems
resources :trips
end
This issue has to do with the clearance gem.
I am not totally familiar with the gem, so as per usual, I checked out the github and found the following:
# config/routes.rb
if Clearance.configuration.routes_enabled?
Rails.application.routes.draw do
resources :passwords,
controller: 'clearance/passwords',
only: [:create, :new]
resource :session,
controller: 'clearance/sessions',
only: [:create]
resources :users,
controller: 'clearance/users',
only: Clearance.configuration.user_actions do
resource :password,
controller: 'clearance/passwords',
only: [:create, :edit, :update]
end
get '/sign_in' => 'clearance/sessions#new', as: 'sign_in'
delete '/sign_out' => 'clearance/sessions#destroy', as: 'sign_out'
if Clearance.configuration.allow_sign_up?
get '/sign_up' => 'clearance/users#new', as: 'sign_up'
end
end
end
This is basically creating the same routes for you, only if the config routes_enabled? is true.
You need to configure clearance as follows to handle the routes yourself:
config.routes = false
After looking at the gems GitHub, it looks like I raked the routes earlier and even though config.routes was set to false in the initializer, there was a conflict generate in the generated resources in production.
I wound up deleting the raked routes and making config.routes=true.

Add custom new routes to rails resources

I my routes file, I have defined a resource
namespace :admin do
resources :invoices, only: [:index, :new]
end
Then I've got a route rule with corresponding path helper new_admin_invoice_path
new_admin_invoice GET /admin/invoices/new(.:format) admin/invoices#new
But how can I add two more new rules, so thуe look like
new_admin_incoming_invoice GET /admin/invoices/new/incoming(.:format) admin/invoices#new {:type=>:incoming}
new_admin_outgoing_invoice GET /admin/invoices/new/outgoing(.:format) admin/invoices#new {:type=>:outgoing}
I tried add them manually
resources :invoices, only: [:index, :new] do
get 'new/incoming', on: :collection, action: :new, type: :incoming
get 'new/outgoing', on: :collection, action: :new, type: :outgoing
end
But got wrong result
new_incoming_admin_invoices GET /admin/invoices/new/incoming(.:format) admin/invoices#new {:type=>:incoming}
new_outgoing_admin_invoices GET /admin/invoices/new/outgoing(.:format) admin/invoices#new {:type=>:outgoing}
How can I get exactly that routes with path helpers what I need?
Here is the easy rails way from official guide
resources :invoices, only: [:index] do
get 'incoming', on: :new, type: :incoming, action: :new
end
Results to
incoming_new_admin_invoice GET /admin/invoices/new/incoming(.:format) admin/invoices#new {:type=>:incoming}
Try by using a scope like this:
scope "/admin" do resources :invoices end

Rails custom and default routes

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.

How to switch route namespace in Rails?

I have the following router:
scope ':name' do # Category :name
get :animators, controller: 'categories'
get :creators, controller: 'categories'
resources :items, only: [:show]
end
it generates the following URLs:
http://localhost:3000/birthday/ # index page
http://localhost:3000/birthday/item/123 # resource show page
However what I would like to do is make my second URL look like this
http://localhost:3000/birthday/animator/123 # resource show page
On my Item model Animator is a :type
If I do it with inner scope
scope ':type' do
resources :items, only: [:show]
end
I will get
http://localhost:3000/birthday/animator/item/123
But I would like to get rid of item, plus it makes me indicate an additional parameter when using link_to in the view, which is not good.
scope ':name' do
get :animators, controller: 'categories'
get :creators, controller: 'categories'
resources :items, only: [:show]
get '/animator/:id' => 'animator#show'
end
resources :items, only: [:show] will only catch routes like /birthday/items/1, /birthday/items/2 etc. While get '/animator/:id' => 'animator#show will be able to catch the routes like you have mentioned in your question:
http://localhost:3000/birthday/animator/123
I've added
get ':type/:slug', to: 'items#show', as: :item
to the scope block. It doesn't look pretty IMO, but I will try to fix that eventually.

How can I specify two actions mapped to the DELETE verb in a rails resource?

I have an api tokens controller based on Matteo Melanis blog post. I'd like to add two custom actions register and unregister to the controller, and so the route that looked like this
namespace :api do
namespace :v1 do
resources :tokens,:only => [:create, :destroy]
end
end
has now become this
namespace :api do
namespace :v1 do
resources :tokens do
put 'register', on: :member, as: :register
delete 'unregister', on: :member, as: :unregister
end
end
end
This is the only way I've found that doesn't let unregister suppress the CRUD destroy action, associated with the DELETE verb. I tried to do
resources :tokens, :only => [:create, :destroy, :register, :unregister] do
in the above code, as well as defining resources :tokens,:only => [:create, :destroy] in parallel to the block. Yet, I either get the undesirable all CRUD + custom actions, or one of the custom actions overriding a CRUD action.
In short, I'd like to end up with
register_api_v1_token PUT /api/v1/tokens/:id/register(.:format) api/v1/tokens#register
unregister_api_v1_token DELETE /api/v1/tokens/:id/unregister(.:format) api/v1/tokens#unregister
api_v1_tokens GET /api/v1/tokens(.:format) api/v1/tokens#index
POST /api/v1/tokens(.:format) api/v1/tokens#create
DELETE /api/v1/tokens/:id(.:format) api/v1/tokens#destroy
Is this possible, and if yes: how can I make it so?
This should work:
resources :tokens, only: [:create, :destroy] do
member do
put 'register'
delete 'unregister'
end
end

Resources