Reputation: 19220
I’m using Rails 4.2.7. I want to redirect the user to a different page if they visit the root of the web site and they are already logged in. So I added this to my config/routes.rb file
authenticated do
root :to => 'my_objects#index', as: :authenticated
end
root 'pages#index'
but when I start my Rails server, I get this error
/Users/davea/Documents/workspace/myproject/config/routes.rb:11:in `block in <top (required)>': undefined method `authenticated' for #<ActionDispatch::Routing::Mapper:0x007fc6753ff230> (NoMethodError)
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.7.1/lib/action_dispatch/routing/route_set.rb:432:in `instance_exec'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.7.1/lib/action_dispatch/routing/route_set.rb:432:in `eval_block'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.7.1/lib/action_dispatch/routing/route_set.rb:410:in `draw'
from /Users/davea/Documents/workspace/myproject/config/routes.rb:1:in `<top (required)>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/application/routes_reloader.rb:40:in `each'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/application/routes_reloader.rb:16:in `reload!'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/application/routes_reloader.rb:26:in `block in updater'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/file_update_checker.rb:75:in `execute'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/application/routes_reloader.rb:27:in `updater'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/initializable.rb:30:in `run'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `call'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
from /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/application.rb:352:in `initialize!'
from /Users/davea/Documents/workspace/myproject/config/environment.rb:5:in `<top (required)>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/davea/Documents/workspace/myproject/config.ru:3:in `block in <main>'
from /Users/davea/.rvm/gems/ruby-2.3.0@global/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
from /Users/davea/.rvm/gems/ruby-2.3.0@global/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
from /Users/davea/Documents/workspace/myproject/config.ru:in `new'
from /Users/davea/Documents/workspace/myproject/config.ru:in `<main>'
from /Users/davea/.rvm/gems/ruby-2.3.0@global/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
from /Users/davea/.rvm/gems/ruby-2.3.0@global/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
from /Users/davea/.rvm/gems/ruby-2.3.0@global/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
from /Users/davea/.rvm/gems/ruby-2.3.0@global/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
from /Users/davea/.rvm/gems/ruby-2.3.0@global/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/server.rb:61:in `app'
from /Users/davea/.rvm/gems/ruby-2.3.0@global/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/server.rb:139:in `log_to_stdout'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/server.rb:78:in `start'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:80:in `block in server'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:75:in `tap'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:75:in `server'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/davea/Documents/workspace/myproject/bin/rails:9:in `require'
from /Users/davea/Documents/workspace/myproject/bin/rails:9:in `<top (required)>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `call'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client/command.rb:7:in `call'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client.rb:30:in `run'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/bin/spring:49:in `<top (required)>'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `load'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `<top (required)>'
from /Users/davea/Documents/workspace/myproject/bin/spring:13:in `require'
from /Users/davea/Documents/workspace/myproject/bin/spring:13:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'
How can I implement the redirection from the root if a user is logged in?
Edit: Here is my complete routes file …
get 'user_my_object_time_matches/create'
get 'my_objects/index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'pages#index'
get '/auth/:provider/callback', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'
delete '/logout', to: 'sessions#destroy'
resources :sessions, only: [:create, :destroy]
resources :users do
collection do
get 'edit'
end
end
resources :my_objects do
collection do
post 'create'
get 'import'
get '/index', to: redirect('/my_objects/index')
get 'search'
get 'stats'
end
end
resources :user_my_object_time_matches
resources :countries do
get :states #-> url.com/countries/:country_id/states/
end
and here is what I updated the pages controller to be …
class PagesController < ApplicationController
def index
redirect_to my_objects_path if logged_in?
end
end
but I’m not getting redirected to the right page. Instead I get redirected to a place that causes a “The action 'show' could not be found for MyObjectsController ” error. Here is the log
Started GET "/" for 127.0.0.1 at 2016-09-10 17:42:33 -0500
Processing by PagesController#index as HTML
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 28]]
Redirected to http://myproject.devbox.com:3000/my_objects/create
Completed 302 Found in 12ms (ActiveRecord: 2.8ms)
Edit 2: Output of rake routes
localhost:myproject davea$ rake routes
Prefix Verb URI Pattern Controller#Action
user_my_object_time_matches_create GET /user_my_object_time_matches/create(.:format) user_my_object_time_matches#create
my_objects_index GET /my_objects/index(.:format) my_objects#index
root GET / pages#index
GET /auth/:provider/callback(.:format) sessions#create
logout GET /logout(.:format) sessions#destroy
DELETE /logout(.:format) sessions#destroy
sessions POST /sessions(.:format) sessions#create
session DELETE /sessions/:id(.:format) sessions#destroy
edit_users GET /users/edit(.:format) users#edit
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
my_objects POST /my_objects/create(.:format) my_objects#create
import_my_objects GET /my_objects/import(.:format) my_objects#import
index_my_objects GET /my_objects/index(.:format) redirect(301, /my_objects/index)
search_my_objects GET /my_objects/search(.:format) my_objects#search
stats_my_objects GET /my_objects/stats(.:format) my_objects#stats
GET /my_objects(.:format) my_objects#index
POST /my_objects(.:format) my_objects#create
new_my_object GET /my_objects/new(.:format) my_objects#new
edit_my_object GET /my_objects/:id/edit(.:format) my_objects#edit
my_object GET /my_objects/:id(.:format) my_objects#show
PATCH /my_objects/:id(.:format) my_objects#update
PUT /my_objects/:id(.:format) my_objects#update
DELETE /my_objects/:id(.:format) my_objects#destroy
user_my_object_time_matches GET /user_my_object_time_matches(.:format) user_my_object_time_matches#index
POST /user_my_object_time_matches(.:format) user_my_object_time_matches#create
new_user_my_object_time_match GET /user_my_object_time_matches/new(.:format) user_my_object_time_matches#new
edit_user_my_object_time_match GET /user_my_object_time_matches/:id/edit(.:format) user_my_object_time_matches#edit
user_my_object_time_match GET /user_my_object_time_matches/:id(.:format) user_my_object_time_matches#show
PATCH /user_my_object_time_matches/:id(.:format) user_my_object_time_matches#update
PUT /user_my_object_time_matches/:id(.:format) user_my_object_time_matches#update
DELETE /user_my_object_time_matches/:id(.:format) user_my_object_time_matches#destroy
country_states GET /countries/:country_id/states(.:format) countries#states
countries GET /countries(.:format) countries#index
POST /countries(.:format) countries#create
new_country GET /countries/new(.:format) countries#new
edit_country GET /countries/:id/edit(.:format) countries#edit
country GET /countries/:id(.:format) countries#show
PATCH /countries/:id(.:format) countries#update
PUT /countries/:id(.:format) countries#update
DELETE /countries/:id(.:format) countries#destroy
Upvotes: 0
Views: 407
Reputation: 20263
Remove the
authenticated do
root :to => 'my_objects#index', as: :authenticated
end
bit from your routes file. And in your PagesController
, do:
class Pages < ApplicationController
def index
redirect_to my_objects_path if authenticated?
... do other stuff as appropriate ...
end
end
This assumes:
You have something like resources :my_objects
in your routes.rb
file. I'm not sure if you're showing your whole routes file. If you don't have something like that in there, you'll need to add it.
You have a method called authenticated?
. That's just an example. Put in whatever you actually have.
Upvotes: 0