我目前在我的rails应用中使用Devise。我想用我的api添加Devise Token Auth进行身份验证。当我尝试使用
设置devise_token_auth时rails g devise_token_auth:install User auth
rake db:migrate
我收到以下错误:
~/workspace (devise_token_auth) $ rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
ArgumentError: Invalid route name, already in use: 'new_user_session'
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:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:557:in `add_route'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1561:in `add_route'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1536:in `decomposed_match'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1534:in `block in decomposed_match'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1419:in `block (2 levels) in member'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:816:in `scope'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1419:in `block in member'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1665:in `with_scope_level'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1415:in `member'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1534:in `decomposed_match'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1517:in `block in match'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1507:in `each'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1507:in `match'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:689:in `map_method'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:650:in `get'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:374:in `block in devise_session'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1198:in `block in resource'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1676:in `block (2 levels) in resource_scope'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:816:in `scope'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1676:in `block in resource_scope'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1665:in `with_scope_level'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1675:in `resource_scope'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:1197:in `resource'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:373:in `devise_session'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:263:in `block (4 levels) in devise_for'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:263:in `each'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:263:in `block (3 levels) in devise_for'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:461:in `with_devise_exclusive_scope'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:262:in `block (2 levels) in devise_for'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:365:in `block in devise_scope'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:940:in `block in constraints'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:816:in `scope'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/mapper.rb:940:in `constraints'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:364:in `devise_scope'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:261:in `block in devise_for'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:237:in `each'
/usr/local/rvm/gems/ruby-2.2.1/gems/devise-3.5.3/lib/devise/rails/routes.rb:237:in `devise_for'
/home/ubuntu/workspace/config/routes.rb:3:in `block in <top (required) '
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:434:in `instance_exec'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:434:in `eval_block'
/usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:412:in `draw'
/home/ubuntu/workspace/config/routes.rb:1:in `<top (required) '
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application/routes_reloader.rb:40:in `each'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application/routes_reloader.rb:40:in `load_paths'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application/routes_reloader.rb:16:in `reload!'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application/routes_reloader.rb:26:in `block in updater'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/file_update_checker.rb:75:in `call'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/file_update_checker.rb:75:in `execute'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application/routes_reloader.rb:27:in `updater'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application/finisher.rb:69:in `block in <module:Finisher '
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/initializable.rb:30:in `instance_exec'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/initializable.rb:30:in `run'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/initializable.rb:55:in `block in run_initializers'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `call'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/initializable.rb:54:in `run_initializers'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application.rb:352:in `initialize!'
/home/ubuntu/workspace/config/environment.rb:5:in `<top (required) '
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application.rb:328:in `require_environment!'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.4/lib/rails/application.rb:457:in `block in run_tasks_blocks'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/usr/local/rvm/rubies/ruby-2.2.1/bin/rake:33:in `<main '
Tasks: TOP = db:migrate = environment
我理解这是因为路线上的冲突。 我如何让它工作? 有没有解决办法?
答案 0 :(得分:6)
为了使用devise_token_auth和devise,我们需要在devise_token_auth初始化程序中设置一个标志,如下所示:
DeviseTokenAuth.setup do |config|
# enable_standard_devise_support = false
end
和路径文件也需要更改
Rails.application.routes.draw do
# standard devise routes available at /users
# NOTE: make sure this comes first!!!
devise_for :users
# token auth routes available at /api/v1/auth
namespace :api do
scope :v1 do
mount_devise_token_auth_for 'User', at: 'auth'
end
end
end
中找到了这个