服务器错误:
ActionController::RoutingError (No route matches [OPTIONS] "/users"): actionpack (4.1.9) lib/action_dispatch/mIDdleware/deBUG_exceptions.rb:21:in `call' actionpack (4.1.9) lib/action_dispatch/mIDdleware/show_exceptions.rb:30:in `call' railtIEs (4.1.9) lib/rails/rack/logger.rb:38:in `call_app' railtIEs (4.1.9) lib/rails/rack/logger.rb:20:in `block in call' activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `block in tagged' activesupport (4.1.9) lib/active_support/tagged_logging.rb:26:in `tagged' activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `tagged' railtIEs (4.1.9) lib/rails/rack/logger.rb:20:in `call' actionpack (4.1.9) lib/action_dispatch/mIDdleware/request_ID.rb:21:in `call' rack (1.5.5) lib/rack/methodoverrIDe.rb:21:in `call' rack (1.5.5) lib/rack/runtime.rb:17:in `call' activesupport (4.1.9) lib/active_support/cache/strategy/local_cache_mIDdleware.rb:26:in `call' rack (1.5.5) lib/rack/lock.rb:17:in `call' actionpack (4.1.9) lib/action_dispatch/mIDdleware/static.rb:84:in `call' rack (1.5.5) lib/rack/sendfile.rb:112:in `call' railtIEs (4.1.9) lib/rails/engine.rb:514:in `call' railtIEs (4.1.9) lib/rails/application.rb:144:in `call' rack (1.5.5) lib/rack/content_length.rb:14:in `call' thin (1.6.3) lib/thin/connection.rb:86:in `block in pre_process' thin (1.6.3) lib/thin/connection.rb:84:in `catch' thin (1.6.3) lib/thin/connection.rb:84:in `pre_process' thin (1.6.3) lib/thin/connection.rb:53:in `process' thin (1.6.3) lib/thin/connection.rb:39:in `receive_data' eventmachine (1.0.8) lib/eventmachine.rb:193:in `run_machine' eventmachine (1.0.8) lib/eventmachine.rb:193:in `run' thin (1.6.3) lib/thin/backends/base.rb:73:in `start' thin (1.6.3) lib/thin/server.rb:162:in `start' rack (1.5.5) lib/rack/handler/thin.rb:16:in `run' rack (1.5.5) lib/rack/server.rb:264:in `start' railtIEs (4.1.9) lib/rails/commands/server.rb:69:in `start' railtIEs (4.1.9) lib/rails/commands/commands_tasks.rb:81:in `block in server' railtIEs (4.1.9) lib/rails/commands/commands_tasks.rb:76:in `tap' railtIEs (4.1.9) lib/rails/commands/commands_tasks.rb:76:in `server' railtIEs (4.1.9) lib/rails/commands/commands_tasks.rb:40:in `run_command!' railtIEs (4.1.9) lib/rails/commands.rb:17:in `<top (required)>' bin/rails:8:in `require' bin/rails:8:in `<top (required)>' spring (1.3.6) lib/spring/clIEnt/rails.rb:28:in `load' spring (1.3.6) lib/spring/clIEnt/rails.rb:28:in `call' spring (1.3.6) lib/spring/clIEnt/command.rb:7:in `call' spring (1.3.6) lib/spring/clIEnt.rb:26:in `run' spring (1.3.6) bin/spring:48:in `<top (required)>' spring (1.3.6) lib/spring/binstub.rb:11:in `load' spring (1.3.6) lib/spring/binstub.rb:11:in `<top (required)>' bin/spring:13:in `require' bin/spring:13:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>'
所以这将是我的Rails服务器控制器和路由文件.
用户控制器
class UsersController < ApplicationController def index respond_to do |format| format.Json {render Json: { name: 'Jonhy'},callback: params[:callback]} end end def new end def create_user puts user_params @user = User.new(user_params) if @user.save render Json: { error: false },layout: false else render Json: { error: true },layout: false end end def show end def user_params params.require(:user).permit(:email,:password,:password_confirmation) endend
应用控制器
class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs,you may want to use :null_session instead. protect_from_forgery with: :exception before_filter :allow_AJAX_request_from_other_domains def allow_AJAX_request_from_other_domains headers['Access-Control-Allow-Origin'] = 'http://localhost:8001' headers['Access-Control-Request-Method'] = '*' headers['Access-Control-Allow-headers'] = '*' endend
击溃
Rails.application.routes.draw do root 'home#index' resources :users do collection { post :create_user,via: :options } endend
这是我的AngularJs,我在那里制作POST AJAX.
var App = angular.module('OutOfBox',['ng.deviceDetector','ngRoute','ngResource']);App.factory('Users',function($resource){ var users = $resource('http://127.0.0.1\:8001/:user',{user:'users'},{ query: {method:'GET',isArray: true},save: {method:'POST',isArray: false} }); return users; });App.controller('MainCtrl',['$scope','$http','Users',function($scope,$http,Users) { $scope.user = []; $scope.responsive = []; $scope.submit = function() { if ($scope.users.email && $scope.users.password && $scope.users.password_confirmation) { $scope.user.push(this.users); Users.save({user: $scope.user},function(){ console.log(':)') },function(error){ console.log(error) }); $scope.users = ''; } };}]);App.config(['$routeProvIDer','$locationProvIDer','$httpProvIDer',function($routeProvIDer,$locationProvIDer,$httpProvIDer) { $routeProvIDer. when('/home',{ templateUrl: 'vIEws/main.HTML',controller: 'MainCtrl' }). otherwise({ redirectTo: '/home' }); // use the HTML5 History API $locationProvIDer.HTML5Mode(true); $httpProvIDer.defaults.useXDomain = true;delete $httpProvIDer.defaults.headers.common["X-Requested-With"]; }]);
所以我尝试了很多解决方案,但没有人修复我的.
解决方法 Rails无法处理[OPTIONS]请求您必须安装rack-cors才能处理CORS.
这是你的问题来自哪里:
resources :users do collection { post :create_user,via: :options } # via: :options ? end总结
以上是内存溢出为你收集整理的ruby-on-rails – 在尝试使用AngularJS将数据发布到RAils服务器时获取ActionController :: RoutingError(没有路由匹配[OPTIONS]“/ users”全部内容,希望文章能够帮你解决ruby-on-rails – 在尝试使用AngularJS将数据发布到RAils服务器时获取ActionController :: RoutingError(没有路由匹配[OPTIONS]“/ users”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)