【问题标题】:rails 4 - no routes matches error with curlrails 4 - 没有路线与 curl 匹配错误
【发布时间】:2014-10-17 02:24:26
【问题描述】:

我正在尝试使用 rails 4 实现 API。

通过浏览器访问API时

http://api.myapp.dev/v1/users/1.json

JSON 响应正确。但是当我尝试使用 curl 获取 JSON 时

curl http://api.myapp.dev/v1/users/1.json

它返回一个html,但基本上错误是no route matches [GET] "users/1"

这是我的路线文件。

Myapp::Application.routes.draw do
  devise_for :users
  #routes for API
  constraints subdomain: 'api' do
    scope module: 'api' do
      namespace :v1 do
        resources :users, :only => [:show]
      end
    end
  end
end

我的控制器是

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

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

当我运行rake routes时,路由存在:

v1_user GET    /v1/users/:id(.:format)    api/v1/users#show {:subdomain=>"api"}

是否需要额外的配置?

更新 我使用prax 作为代理服务器

【问题讨论】:

  • 在浏览器发出请求之前,在 chrome 中打开检查器并转到网络选项卡,然后转到网络选项卡。右键单击请求并选择“复制为 curl 请求”并尝试
  • 我试了一下没有结果。卷曲什么也没返回。
  • 现在正在工作。我清理浏览器缓存并重新启动 prax。

标签: ruby-on-rails ruby json ruby-on-rails-4 curl


【解决方案1】:

我认为您需要告诉您接受json 回复:

curl -H "Accept: application/json" http://api.myapp.dev/v1/users/1.json

【讨论】:

  • 谢谢,但是 curl 返回 curl: (52) Empty reply from server。我正在使用 prax。
  • 现在正在工作。我清理了浏览器缓存并且它可以工作,即使没有标题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
相关资源
最近更新 更多