我正在使用Rails 4.2.7。我想将用户重定向到另一个页面,如果他们访问网站的根目录并且他们已经登录。所以我将其添加到我的config / routes.rb文件
authenticated do
root :to => 'my_objects#index', as: :authenticated
end
root 'pages#index'
但是当我启动Rails服务器时,我收到此错误
/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>'
如果用户已登录,如何从根目录实施重定向?
编辑:这是我的完整路线档案......
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
以下是我更新页面控制器的内容......
class PagesController < ApplicationController
def index
redirect_to my_objects_path if logged_in?
end
end
但我没有被重定向到正确的页面。相反,我被重定向到一个导致“动作&#39;显示&#39;找不到MyObjectsController “错误。这是日志
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)
编辑2:佣金路线的输出
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
答案 0 :(得分:0)
删除
if abs((ssptime - timecode).total_seconds()) < margin:
来自您的路线文件的位。在您的 authenticated do
root :to => 'my_objects#index', as: :authenticated
end
中,执行:
PagesController
这假定:
class Pages < ApplicationController
def index
redirect_to my_objects_path if authenticated?
... do other stuff as appropriate ...
end
end
文件中有resources :my_objects
之类的内容。我不确定你是否在显示你的整个路线文件。如果你没有那样的东西,你需要添加它。
您有一个名为routes.rb
的方法。这只是一个例子。放入你实际拥有的任何东西。