我在Ruby on Rails应用程序中使用angular作为前端
angular.module('rails_api')
.controller('AuthController', ['$scope', '$location', '$http', 'Auth', function($scope, $location, $http, Auth){
//auth
var config = {
headers: {
'X-HTTP-Memthod-Override': 'POST'
}
}
$scope.login = function () {
Auth.login($scope.user, config).then(function(user) {
console.log(user);
}, function(error) {
});
$scope.$on('devise:login', function(event, currentUser){
$location.path('/');
});
$scope.$on('devise:new-session', function(event, currentUser) {
// user logged in by Auth.login({...})
});
}
$scope.signup = function () {
Auth.register($scope.user, config).then(function(registeredUser) {
console.log(registeredUser); // => {id: 1, ect: '...'}
}, function(error) {
// Registration failed...
});
$scope.$on('devise:new-registration', function(event, user) {
$location.path('/');
});
}
$scope.logout = function () {
var config = {
headers: {
'X-HTTP-Method-Override': 'DELETE'
}
};
// Log in user...
// ...
Auth.logout(config).then(function(oldUser) {
//console.log("oldUser");
//alert(oldUser.name + "you're signed out now.");
}, function(error) {
console.log(error);
});
$scope.$on('devise:logout', function(event, oldCurrentUser) {
$location.path('/');
});
}
}]);
..
<li><a href="/auth/login"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
<li><a href="/auth/signup"><span class="glyphicon glyphicon-registration-mark"></span> Sign UP</a></li>
<li><a href="" ng-controller="AuthController" ng-click="logout()"><span class="glyphicon glyphicon-registration-mark" target="_self"></span> Logout</a></li>
登录和注册功能可以正常工作,但注销无效,我的服务器显示此错误
Started DELETE "/users/sign_out.json" for 127.0.0.1 at 2016-05-06 10:17:26 +0300
ActionController::RoutingError (No route matches [DELETE] "/users/sign_out.json"):
actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.6) lib/rails/engine.rb:518:in `call'
railties (4.2.6) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/user/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/home/user/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/home/user/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'
这是我的Gemfile
source 'https://rubygems.org'
ruby '2.3.0'
gem 'rails', '4.2.6'
gem 'pg', '~> 0.15'
gem 'sass-rails', '~> 5.0'
gem 'bootstrap-sass'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'rails_admin'
gem 'devise'
gem "bower-rails", "~> 0.10.0"
gem 'angular-rails-templates'
gem 'angular_rails_csrf'
gem 'rack-cors', :require => 'rack/cors'
gem 'faker'
gem 'pry'
gem 'carrierwave'
gem 'dropzonejs-rails'
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'jasmine'
end
group :development do
gem 'web-console', '~> 2.0'
gem 'spring'
end
gem 'rails_12factor', group: :production
source "https://rails-assets.org" do
gem "rails-assets-angular-devise"
end
rake routes
Prefix Verb URI Pattern Controller#Action
images GET /images(.:format) images#index
POST /images(.:format) images#create
new_image GET /images/new(.:format) images#new
edit_image GET /images/:id/edit(.:format) images#edit
image GET /images/:id(.:format) images#show
PATCH /images/:id(.:format) images#update
PUT /images/:id(.:format) images#update
DELETE /images/:id(.:format) images#destroy
welcome_index GET /welcome/index(.:format) welcome#index
rails_admin /admin RailsAdmin::Engine
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
api_v1_posts GET /api/v1/posts(.:format) api/v1/posts#index
POST /api/v1/posts(.:format) api/v1/posts#create
new_api_v1_post GET /api/v1/posts/new(.:format) api/v1/posts#new
edit_api_v1_post GET /api/v1/posts/:id/edit(.:format) api/v1/posts#edit
api_v1_post GET /api/v1/posts/:id(.:format) api/v1/posts#show
PATCH /api/v1/posts/:id(.:format) api/v1/posts#update
PUT /api/v1/posts/:id(.:format) api/v1/posts#update
DELETE /api/v1/posts/:id(.:format) api/v1/posts#destroy
api_v1_users GET /api/v1/users(.:format) api/v1/users#index
POST /api/v1/users(.:format) api/v1/users#create
new_api_v1_user GET /api/v1/users/new(.:format) api/v1/users#new
edit_api_v1_user GET /api/v1/users/:id/edit(.:format) api/v1/users#edit
api_v1_user GET /api/v1/users/:id(.:format) api/v1/users#show
PATCH /api/v1/users/:id(.:format) api/v1/users#update
PUT /api/v1/users/:id(.:format) api/v1/users#update
DELETE /api/v1/users/:id(.:format) api/v1/users#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session GET /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / welcome#index
GET /*path(.:format) welcome#index
Routes for RailsAdmin::Engine:
dashboard GET / rails_admin/main#dashboard
index GET|POST /:model_name(.:format) rails_admin/main#index
new GET|POST /:model_name/new(.:format) rails_admin/main#new
export GET|POST /:model_name/export(.:format) rails_admin/main#export
bulk_delete POST|DELETE /:model_name/bulk_delete(.:format) rails_admin/main#bulk_delete
bulk_action POST /:model_name/bulk_action(.:format) rails_admin/main#bulk_action
show GET /:model_name/:id(.:format) rails_admin/main#show
edit GET|PUT /:model_name/:id/edit(.:format) rails_admin/main#edit
delete GET|DELETE /:model_name/:id/delete(.:format) rails_admin/main#delete
show_in_app GET /:model_name/:id/show_in_app(.:format) rails_admin/main#show_in_app
routes.rb
Rails.application.routes.draw do
resources :images
get 'welcome/index'
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :posts
namespace :api do
namespace :v1 do
resources :posts
resources :users
end
end
devise_for :users
root to: 'welcome#index'
get '*path' => "welcome#index"
end
所以我真的不明白这个问题是什么
答案 0 :(得分:0)
除非另有说明,路由文件定义您的案例中的路由,仅用于HTML
个请求,而不用于JSON
请求。但您提出的请求是:Started DELETE "/users/sign_out.json" for 127.0.0.1 at 2016-05-06 10:17:26 +0300
以格式JSON
请求数据,而您的路由文件并不理解。
现在,您有两种方法可以摆脱这个错误:
1)确保您以HTML
格式申请,而不是通过代码以JSON
格式申请。
2)您可以修改路线,以便它们也响应JSON
格式。以下是您将如何完成任务:
resources :users, :only => [:show], :defaults => { :format => 'json' }
答案 1 :(得分:0)
问题是您必须已指定要使用GET
方法在config/initializer/devise.rb
文件中注销。
确保初始值设定项中没有此行:
config.sign_out_via = :get
答案 2 :(得分:0)
在您的 routes.rb 文件中,使用 PUT 和 DELETE 等特殊方法明确定义路由。例如
devise_scope :user do
delete '/sign_out' => 'sessions#destroy', :defaults => { :format => 'json' }
put '/signup' => 'registrations#update', :defaults => { :format => 'json' }
end
尽管这些路由已经由“devise_for :users”创建并在邮递员上工作,但没有明确创建它们,我的 RSPec 测试失败了。