【发布时间】:2016-10-12 08:53:24
【问题描述】:
我正在尝试将移动客户端与 REST API 配对,以便获得可以在应用程序中显示的 JSON 数据。我正在关注 Abraham Kuri Vargas 的 Apis on Rails。在 API 中,我创建了一个子域以及一些版本控制,因此控制器文件位于 app/controllers/api/v1 中。当我在本地运行 API 时,它会返回所需的 JSON 数据。我面临的问题是,每当我将 API 部署到 Heroku/AWS 上时,它都会显示“找不到应用程序”。是远程服务器没有找到进入 app/controllers/api/v1 的方式吗?是否有任何解决方案可以解决此类问题,因为即使在书中作者也写过“由于应用程序的结构,我们不会将应用程序部署到任何服务器”。我真的被这个问题困扰了很长时间,并且希望有任何建议!
app/controllers/api/v1/users_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
config/routes.rb
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
输出,当我在本地运行 localhosts://3000/users/1 时
{"id":1,"email":"example@marketplace.com","created_at":"2016-06-09T05:36:06.606Z","updated_at":"2016-06-09T05: 36:06.606Z"}
我运行 petoye.herokuapp.com/users/1 时的 Heroku 服务器日志
于 2016 年 6 月 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_dispatch/middleware/debug_exceptions.rb:21:in call'call_app'
vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/rack/logger.rb:38:in
vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in block in tagged'tagged' vendor/bundle/ruby/2.2.0/gems/railties-4.2。 6/lib/rails/rack/logger.rb:20:in
vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:incall'call'
vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in
vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in call'call'
2016-06-12T06:43:36.946702+00:00 app[web.1]: vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in
vendor/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/server.rb:569:in handle_request'block in spawn_thread'
vendor/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/thread_pool.rb:114:in
【问题讨论】:
-
您可以根据您的请求从服务器发布日志吗?
-
用服务器日志更新了帖子。你会看看吗?谢谢。我注意到的一件事是,即使我在本地使用 puma 运行相同的东西,我也会得到相同的错误。但是,当我使用 WEBrick 时没有问题。
-
是的,您的 puma 服务器似乎无法运行
-
我不认为 puma 服务器用于托管 Rails API。在使用任何 Rails 应用程序时都可以正常工作。
-
应该没问题。这可能会有所帮助devcenter.heroku.com/articles/…
标签: ruby-on-rails json heroku puma rails-api