Rails - Deploying to Heroku with duplicate routes - ruby-on-rails

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.

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
...

Clearance gem redirect issues

Hello I'am using clearance gem for user authentication.
So far everything works perfect, but I'am confused why even after setting the root routes for my pages index view, it still redirects me to the sign_in page of clearance gem.
I have looked at github documentation notes, and it list no other work around for this. I'am doing something wrong?
Here is what my routes.rb file looks like:
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"
root 'pages#index'
# For details on the DSL available within this file, see
http://guides.rubyonrails.org/routing.html
end
the clearance require_login before_action must be active for that route. Did you add that before action to application controller? Does PagesController inherit from ApplicationCobtroller? If so, you need to add skip_before_action :require_login to your pages controller. Perhaps scoped to the particular pages controller action you want unprotected.

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.

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)

Ruby on Rails attribute routing using resources

I'm working on a preservation project where I have models for Institutions, Objects, and Files and I'm using resources in my routes file to help manage all of that. The problem I'm having is that resources uses the :id generated by Ruby on Rails for all of its routes and I would like to use a different attribute. This is particularly important for academic preservation and URL consistency. For example, I would like my show URL for an institution to read institutions/:identifier as opposed to institutions/:id, where :identifier is the attribute I have created. The problem is, I haven't been able to find any information on how to do this aside from just matching individual routes to the new ones I want which seems like a cheap hack at best and breaks a lot of my code.
This is my current routes file:
Fluctus::Application.routes.draw do
#match "institutions/", to: 'institutions#index', via: [:get]
#match "institutions/", to: 'institutions#create', via: [:post]
#match "institutions/:identifier", to: 'institutions#show', via: [:get]
#match "institutions/:identifier", to: 'institutions#update', via: [:patch]
#match "institutions/:identifier", to: 'institutions#update', via: [:put]
#match "institutions/:identifier", to: 'institutions#destroy', via: [:delete]
#match "institutions/:identifier/edit", to: 'institutions#edit', via: [:get]
#match "institutions/:identifier/events", to: 'events#index', via: [:get]
#match "institutions/new", to: 'institutions#new', via: [:get]
#
#match "objects/institution_identifier", to: 'intellectual_objects#index', via: [:get], as: "intellectual_objects_path"
#match "objects/institution_identifier", to: 'intellectual_objects#create', via: [:post]
resources :institutions do
resources :intellectual_objects, only: [:index, :create], path: 'objects'
resources :events, only: [:index]
end
resources :intellectual_objects, only: [:show, :edit, :update, :destroy], path: 'objects' do
resources :generic_files, only: :create, path: 'files'
patch "files/:id", to: 'generic_files#update', constraints: {id: /.*/}, trailing_slash: true, format: 'json'
resources :events, only: [:create, :index]
end
devise_for :users
resources :users do
patch 'update_password', on: :collection
get 'edit_password', on: :member
patch 'generate_api_key', on: :member
end
resources :generic_files, only: [:show, :destroy], path: 'files' do
resources :events, only: [:create]
end
Blacklight.add_routes(self)
mount Hydra::RoleManagement::Engine => '/'
authenticated :user do
root to: "institutions#show", as: 'authenticated_root'
# Rails 4 users must specify the 'as' option to give it a unique name
# root :to => "main#dashboard", :as => "authenticated_root"
end
root :to => "catalog#index"
end
You can see where I have attempted to match individual routes without much luck. I've also looked into FriendlyID without any luck so far. If anyone has any ideas it would be greatly appreciated. Thank you!
Unless I'm missing something here, you should just be able to stick the string you want to use into the URL where the id whould be. For example, /objects/show/1 would become /objects/show/some_string. Then in your controller you can find the object using the id parameter:
def show
#object = Object.where(:some_attribute => params[:id]).first
end

Resources