Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Heroku/AWS Elastic Beanstalk上部署Rails API_Ruby On Rails_Json_Heroku_Puma_Rails Api - Fatal编程技术网

Ruby on rails 在Heroku/AWS Elastic Beanstalk上部署Rails API

Ruby on rails 在Heroku/AWS Elastic Beanstalk上部署Rails API,ruby-on-rails,json,heroku,puma,rails-api,Ruby On Rails,Json,Heroku,Puma,Rails Api,我正在尝试将一个移动客户端与一个RESTAPI配对,以便获得可以在应用程序中显示的JSON数据。我正在关注Abraham Kuri Vargas的Rails API。在API中,我创建了一个子域和一些版本控制,因此控制器文件位于app/controllers/API/v1中。当我在本地运行API时,它会返回所需的JSON数据。我面临的问题是,每当我将API部署到Heroku/AWS上时,它都会显示“未找到应用程序”。是因为远程服务器没有进入app/controllers/api/v1吗?这样的问

我正在尝试将一个移动客户端与一个RESTAPI配对,以便获得可以在应用程序中显示的JSON数据。我正在关注Abraham Kuri Vargas的Rails API。在API中,我创建了一个子域和一些版本控制,因此控制器文件位于app/controllers/API/v1中。当我在本地运行API时,它会返回所需的JSON数据。我面临的问题是,每当我将API部署到Heroku/AWS上时,它都会显示“未找到应用程序”。是因为远程服务器没有进入app/controllers/api/v1吗?这样的问题有没有解决办法,因为即使在作者写的书中,“由于应用程序的结构,我们也不会将应用程序部署到任何服务器”。我真的被这个问题困扰了很长一段时间,希望能得到任何建议

app/controllers/api/v1/users\u controller.rb

    class Api::V1::UsersController < ApplicationController
     respond_to :json

     def show
      respond_with User.find(params[:id])
     end

     def create
     user = User.new(user_params)
      if user.save
       render json: user, status: 201, location: [ :api,user ]
      else
       render json: { errors: user.errors }, status: 422
      end
     end

     def update
     user = User.find(params[:id])
      if user.update(user_params)
       render json: user, status: 200, location: [ :api,user ]
      else
       render json: { errors: user.errors }, status: 422
      end
     end

     def destroy
      user = User.find(params[:id])
      user.destroy
      head 204
     end

     private
      def user_params
       params.require(:user).permit(:email,:password,:password_confirmation)
      end
    end
    require 'api_constraints'

    Rails.application.routes.draw do

      devise_for :users
      namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/'  do
        scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
          #resources 
          resources :users, :only => [:show, :create, :update, :destroy]
        end 
      end
    end
输出,当我运行l时ocalhosts://3000/users/1 本地

{“id”:1,“电子邮件”:example@marketplace.com,“创建时间”:“2016-06-09T05:36:06.606Z”,“更新时间”:“2016-06-09T05:36:06.606Z”}

当我运行petoye.herokuapp.com/users/1时,Heroku服务器会记录日志

于2016-06-12 06:43:36+0000开始获取120.60.22.244的“/users/1”
ActionController::RoutingError(没有与[GET]“/users/1”匹配的路由):
供应商/bundle/ruby/2.2.0/gems/actionpack-4.2.6/lib/action\u dispatch/middleware/debug\u exceptions.rb:21:in
call'
vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/rack/logger.rb:38:in
call_app'
供应商/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active\u support/taged\u logging.rb:68:in
block in taged'
vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active\u support/taged\u logging.rb:68:in
taged'vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/rack/logger.rb:20:in
call'
供应商/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in
call'
供应商/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in
call'
2016-06-12T06:43:36.946702+00:00应用程序[web.1]:供应商/捆绑包/ruby/2.2.0/gems/activesupport-4.2.6/lib/active\u support/cache/strategy/local\u cache\u中间件。rb:28:in
call'
vendor/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/server.rb:569:in
handle\u request'

vendor/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/thread\u pool.rb:114:in
block in spawn\u thread'

heroku的问题是,如果你不支付域名费用,只使用基本服务,它会为你的应用程序分配一个子域名。所以我需要做的就是将heroku重定向到一个自定义url,比如api.any_domain.com

更简单的替代方法是覆盖routes.rb文件中api的约束:

  namespace :api, defaults: { format: :json }, path: '/api'  do

现在,您可以在如何为Rails API配置Heroku的步骤下查看API

  • 首先,您需要使用任何注册器购买一个域,比如说您购买了www.example.com,这样您就可以使用像api.example.com这样的子域。完成后,您需要告诉heroku在api.example.com上侦听请求。这可以通过在cmd上键入此命令轻松完成

    heroku domains:add api.example.com
    
  • 使用domains:add命令后,您必须将DNS提供商指向Heroku提供的域的DNS目标。这是通过在example.herokuapp.com(键入heroku info以获取应用程序的名称)上创建一个CNAME条目来实现的。如果使用DNSimple()之类的服务,则上述过程将变得简单。基本上,您添加了一个api.example.com的CNAME记录,该记录指向example.herokuapp.com

  • 一旦部署到heroku,每个人都会忘记一件重要的事情,那就是运行heroku run rake db:migrate命令

  • 完成所有这些之后,您将能够使用api.example.com/any_endpoints访问api

    额外资源:

    您可以根据请求从服务器发布日志吗?已使用服务器日志更新日志。你能看一下吗?谢谢我注意到的一件事是,即使我在本地用puma运行相同的程序,我也会得到相同的错误。但是,当我使用WEBrick时,没有问题。是的,看起来你在运行puma服务器时遇到了问题。我不认为puma服务器是用来承载Rails API的。当使用任何rails应用程序时,它都可以正常工作。应该可以。这可能会有帮助