ruby-on-rails - 带有 Rails API 的 Ember POST 405(不允许)

标签 ruby-on-rails heroku ember.js ember-cli

我收到了 POST https://heroku_client_path.herokuapp.com/login 405 (Not Allowed)对于带有 Rails api 的 Ember.js 应用程序(均在 heroku 上)

处理登录或注册时。我觉得请求 URL 应该是 Heroku 服务器路径/login,因为我设置了 ADAPTER_URL heroku 配置变量,而不是上面显示的 heroku 客户端 URL。

我相信我的 CORS 设置正确。

我正在使用 Ember-CLI。我手动滚动了身份验证,它不是简单例份验证。

环境.js:

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'client-rantly',
    environment: environment,
    baseURL: '/',
    adapterURL: process.env.ADAPTER_URL,
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
      }
    },
  }; 

  if (environment === 'development') {
  }

  if (environment === 'production') {   
  }

  return ENV;
};

适配器/application.js:
import DS from 'ember-data';
import ENV from '../config/environment';

export default DS.ActiveModelAdapter.extend({
  host: ENV.adapterURL || ENV.ADAPTER_URL,
  headers: function () {
    return {
      'auth_token': localStorage.getItem('authToken')
    };
  }.property('authToken')
});

brocfile.js
var EmberApp = require('ember-cli/lib/broccoli/ember-app');

var app = new EmberApp({
  dotEnv: {
    clientAllowedKeys: ['ADAPTER_URL']
  }
});
module.exports = app.toTree();

Controller /application.js
import Ember from 'ember';

export default Ember.Controller.extend({
  needs: ['search'],
  isAuthenticated: false,

  init: function() {
    var authToken = localStorage.getItem('authToken');
    if(authToken) {
      this.isAuthenticated = true;
    }
  },

  actions: {

    login: function () {
      var credentials = {
        email: this.get('email'),
        password: this.get('password')
      };

      this.set('errorMessage', null);
      return Ember.$.post('login', credentials).then(function(response){
        this.set('errorMessage', response.error);
        if (response.auth_token) {
          localStorage.setItem('authToken', response.auth_token);
          localStorage.setItem('userId', response.user_id);
          this.set('isAuthenticated', true);
          location.reload();
        }
      }.bind(this));
    },
  }
});

cors rails 侧 - config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'

Bundler.require(*Rails.groups)

module ServerRantly
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true
    config.autoload_paths << Rails.root.join('lib')

    config.middleware.insert_before 0, "Rack::Cors", :debug => true, :logger => (-> { Rails.logger }) do
      allow do
        origins '*'

        resource '/cors',
          :headers => :any,
          :methods => [:post],
          :credentials => true,
          :max_age => 0

        resource '*',
          :headers => :any,
          :methods => [:get, :post, :delete, :put, :options, :head],
          :max_age => 0
      end
    end
  end
end

最佳答案

在这一行:

adapterURL: process.env.ADAPTER_URL

在哪里process.env.ADAPTER_URL定义?

似乎您在正确的 rails 上覆盖 hostActiveModelAdapter适配器以使用您的非客户端 URL。

关于ruby-on-rails - 带有 Rails API 的 Ember POST 405(不允许),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315771/

相关文章:

未应用 jquery ui 自动完成的 css

node.js - Express Node 用作支持 pushState 的服务器,服务于任何没有任何路径前缀的静态资源

ruby-on-rails - 如何将根据关系计数过滤的结果返回到 RAILS 中的 View ?

ruby-on-rails - 回形针 - 将 tiff 图像转换为 jpg 或 png

Heroku 数据库 :push fails with segfault

python - 如何在 Heroku 上的特定目录 (/app/assets/chromedriver) 中安装 chromedriver?

twitter-bootstrap - 我什么时候应该在 Emberjs 动态添加的 HTML 代码上调用 Twitter Bootstrap 方法?

javascript - 如何在 ember-file-upload 中执行组件操作而不是路由操作

ruby-on-rails - cucumber 加载错误 activesupport

node.js - 使用 Redis 的 Heroku Node.js 应用程序上的 WebSocket 和多个 Dyno