Heroku routing errors in Rails app - ruby-on-rails

On my local machine, when I authenticate at http://subdomain.myapp.dev (using the Pow server), I am properly redirected to the index page.
When logging into the production domain http://subdomain.myapp.com (hosted on Heroku), I am also able to properly authenticate; however, Heroku is not redirecting to the index page. After submitting the necessary credentials, I receive the 'Signed in successfully' notification but remain on the sign in page.
rake routes and heroku run rake routes return identical routing schemes. I've also listed the contents of my routes.rb file below
Example::Application.routes.draw do
devise_scope :user do
authenticated :user do
root :to => 'admin/servers#index'
end
unauthenticated :user do
root :to => 'devise/sessions#new'
end
end
resources :server_imports
resources :servers
devise_for :users
ActiveAdmin.routes(self)
end
Below are the logs after entering the credentials for signing in:
2013-10-12T01:59:32.110046+00:00 app[web.1]: Started POST "/users/sign_in"
2013-10-12T01:59:32.529842+00:00 app[web.1]: Started GET "/"
And here is the first line from heroku run rake routes
root GET / admin/servers#index
As mentioned above, I still get re-routed to the sign in page after successful authentication. I am confused why this problem is only experienced on Heroku and not on my local machine

Why not do something like this:
def after_sign_in_path_for(resource)
admin_servers_index_path
end
See: https://github.com/plataformatec/devise/wiki/How-To%3A-Redirect-to-a-specific-page-on-successful-sign-in-and-sign-out

Related

Ruby on Rails Devise Always localhost: 3000 will be redirected

I accessed localhost/3000/login_users/sign_up and registered a user.
After that, I accessed localhost/3000/login_users/sign_in. However, it will always be redirected to localhost:3000.
I will post routes.rb. I will post the serverlog.
Why is that?
Rails.application.routes.draw do
devise_for :login_users
# For details on the DSL available within this file,
# see http://guides.rubyonrails.org/routing.html
end
you can redirect user to a specific path after sign in by defining a method in application controller.
def after_sign_in_path_for(resource)
some_path
end
by default devise redirect to root path. you can set a root path is routes.rb also as following:
root :to => 'testers#index'

`add_route': Invalid route name, already in use: 'root' (ArgumentError)

Im using rails 4.1.1 and ruby 2.1.1 and am having an issue with devise, namely my routes..I have used this many times before
devise_for :users
get 'pages/index'
# Route to Devise Login Page
devise_scope :user do
root to: "devise/sessions#new"
end
# Directing the user after login
authenticated :user do
root :to => 'pages#index'
end
But i get the error
`add_route': Invalid route name, already in use: 'root' (ArgumentError)
when trying to start the server.. I can see that root is being used twice, but like i said i have been able to do this in the past.. Is there a way around this
Thanks
Found this helpful comment here on stackoverflow
For Rails 4.0 you have to make sure you have unique names for the path
helpers, like root to: "dashboard#show", as: :authenticated_root.
Otherwise the authenticated root and the normal root route end up
having the same name for their path helpers, which Rails 4.0 no longer
allows
so I changed my authenticated root to helper like so
# Directing the user after login
authenticated :user do
root :to => 'pages#index', as: :authenticated_root
end

Route appears in rake routes, but not on site

I'm new to ruby on rails, and I'm having trouble getting up to speed. I've set up devise for user authentication, and everything seemed to be going well. Until I tried to get routes set up
Basically, I've deleted my /public/index.html, and I've set a new default route. Here's my /config/routes.rb
MyApp::Application.routes.draw do
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
end
Okay, so that seems simple enough. I generated a home controller/view. My index.html.erb is just the default. My controller is also blank.
When I run rake routes, I can see the route to the root.
root / home#index
root / home#index
So this seems like a go! But...It's not. I get an error when I try and open my index
No route matches [GET] "/"
I don't feel like the details of my server should be significant, but it's nginx. I can post my server configuration if anyone thinks it's important.
Blargh. I'm an idiot. The server configuration I though wouldn't matter was the problem. I had edited my nginx settings, but I left the default in Unicorn. I never noticed before now, since the /public/index.html in both the unnecessary app and my app were the same. Thanks for all the help though.
You should be add resources :homes
MyApp::Application.routes.draw do
resources :homes
root :to => "home#index"
devise_for :users
end

Trying to set root to devise/sessions#new results in routes mapping error

Going demented with an issue here :-(
My requirement is as follows,
If a user visits myapp.com the root of my app is defaulted to promotional pages & sign-up form. This is achieved by checking for the presence of a subdomain.
If the user is not logged in and tries to visit their account at test.myapp.com they will be directed to test.myapp.com/users/sign_in -aka- devise/sessions#new
If the user is logged in (devise) and visits test.myapp.com the root of the application will be the application dashboard.
Here is what I am trying to use in my routes.rb
constraints(Subdomain) do
authenticated do
root :to => 'dashboard#index'
end
root :to => 'devise/sessions#new'
end
root :to => 'promo_pages#index'
Currently I have the following, you will note that the devise bit is not included.
constraints(Subdomain) do
authenticated do
root :to => 'dashboard#index'
end
end
root :to => 'promo_pages#index'
My problem with the latter is that when a user who is not logged in first visits test.myapp.com they are redirected to test.myapp.com/users/sign_in and an error message is displayed saying "You need to sign in or sign up before continuing." This is because I am enforcing a logon requirement for the dashboard pages.
However I don't want the user to get an error message the first time they visit the page, as it is ugly and makes it look like they have done something wrong when they have not.
My expectation is that if the user is not logged in then they will be directed straight to the logon page and not get an error notification. But when I use my amended version the following happens,
I can visit myapp.com just fine and it is routed to the promo pages
but if I try to visit test.myapp.com I get the following message in the browser
**Unknown action**
Could not find devise mapping for path "/". Maybe you forgot to wrap your route inside the scope block? For example: devise_scope :user do match "/some/route" => "some_devise_controller" end
Please advise what I am doing wrong (if anything) as I am going crackers trying to understand what to do.
Ps: I have found similar errors in stackoverflow and various googling but the solutions just don't seem to work for me. I expect that the solution to the problem lies in the error message that I have included above, but I can't figure out how to apply it.
Finally here is the log entry version of the error above, it is in an easier to read form.
Started GET "/" for 127.0.0.1 at 2012-01-15 21:44:42 +0000
Processing by Devise::SessionsController#new as HTML
[Devise] Could not find devise mapping for path "/".
Maybe you forgot to wrap your route inside the scope block? For example:
devise_scope :user do
match "/some/route" => "some_devise_controller"
end
Completed 404 Not Found in 1ms
AbstractController::ActionNotFound (Could not find devise mapping for path "/".
Maybe you forgot to wrap your route inside the scope block? For example:
devise_scope :user do
match "/some/route" => "some_devise_controller"
end
):
All help is appreciated, and additional details can be provided.
Thanks for reading
Update
I have just noticed that the 'authenticated' check does not appear to be working.
If it was working correctly then when using the second batch of working config, the logged in user visiting test.myapp.com would always be directed to the promo_pages, whereas at the moment he is able to access the dashboard..
I found the devise 'authenticated' method here
https://github.com/plataformatec/devise/pull/1147
Without the constraint, this will cause a Could not find devise mapping for path "/" error. This simple addition fixed it.
devise_scope :user do
authenticated :user do
root :to => 'dashboard#index'
end
unauthenticated :user do
root :to => 'devise/sessions#new'
end
root :to => 'dashboard#index'
end
I have got it sorted,
Ok, firstly the reason I was getting the devise error was that I needed to place the devise root statement inside the "devise_for :users" block
Secondly,
The authenticated check was not working because I failed to include a scope as I was under the mistaken impression it was not necessary.
Here is the finalised code, note that in rails routing the priority is based on order of creation, first created is highest priority. Thus in this case the promo_pages controller is only considered root if nothing else was previously specified.
constraints(Subdomain) do
authenticated :user do
root :to => 'dashboard#index'
end
unauthenticated :user do
root :to => 'devise/sessions#new'
end
end
root :to => 'promo_pages#index'

Routing error on Heroku (after logging in via Devise)

For my rails 3 app I have a route setup as follows
namespace :user do
root :to => "reading_schedules#index"
end
This is what my "rake routes" shows
user_root /user(.:format) {:controller=>"user/reading_schedules", :action=> "index }
Everything works fine on my localmachine. But as soon as I push the site up to Heroku and login I get the following error in my logs
ActionController::RoutingError (uninitialized constant User::ReadingSchedulesController):
If I navigate to the root of the site everything else works fine. But this one url doesn't work. The url it's trying to hit is website/user but like I said, it works fine on my localmachine.
EDIT:
Here's the rest of my routes file
devise_for :users, :path => 'accounts'
root :to => "home#landing"
namespace :user do
root :to => "reading_schedules#index"
end
resources :users do
resources :reading_schedules
member do
get :change_password
post :change_password
end
end
resources :reading_schedules do
member do
get :recalculate
end
end
I found this question first in looking for an answer to the same issue. For any future searchers, check out this link.
For me, it was a combination of the last two answers in this post (adapted for my controllers of course.)
Also of note, this corrected the issue without having to reset the database.
ActionController::RoutingError (uninitialized constant User::UsersController) in heroku (but everything works in local)
Probably not the answer you are looking for but
heroku rake db:reset
solved the problem for me. I didn't have any critical data in the
db so it was not a problem.

Resources