Rails坚持在回复中返回html

时间:2015-10-10 23:28:00

标签: ruby-on-rails rest ruby-on-rails-4.2 actionpack

我一直在尝试配置rails restful API服务。

我已使用Rails-API设置项目,并使用this添加了使用设计的令牌身份验证。我的目标是所有响应都采用JSON格式,这就是我认为rails-api将要实现的目标

运行设计rake生成器后,我尝试与注册端点进行交互,我对服务器的curl请求执行此操作:

curl --form "email=foo@email.com" --form "password=abc123" --form "password_confirmation=abc123" -H "Accept: application/json" -H "Content-type: application/json" -X POST --dump-header headers http://api.local.dev:3000/

我收到了这个回复:

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Action Controller: Exception caught</title>
  <style>
    body {
      background-color: #FAFAFA;
      color: #333;
      margin: 0px;
    }
.....

页面在渲染时只是一个异常跟踪:

rendered html response

在服务器输出上,这些行显示actionpack中间件仍在倾倒他的犯规,未经请求的跟踪html:

 Rendered /home/diffeo/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.6ms)
  Rendered /home/diffeo/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
  Rendered /home/diffeo/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
  Rendered /home/diffeo/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (20.0ms)

我想了解的是如何完全禁用所有内容的HTML回复

供参考,这是我的Gemfile

source 'https://rubygems.org'


gem 'rails', '4.2.1'

gem 'rails-api'

gem 'pg'
gem 'activerecord-postgis-adapter'
gem 'rgeo'
gem 'devise'
gem 'devise_token_auth' # Token based authentication for Rails JSON APIs
gem 'omniauth' # required for devise_token_auth

# To use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano', :group => :development

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

group :development, :test do
    gem 'pry-byebug', '=1.3.3'
    gem 'pry-stack_explorer'
    gem 'pry-rails'
    gem 'pry-remote'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'

  gem "rspec-rails", "~> 3.3"
end

group :test do

  #gem "shoulda-matchers"
  gem "factory_girl_rails"
  gem 'ffaker'
end

这是我的routes.rb

Rails.application.routes.draw do
  namespace :api, constraints: { format: 'json' } do
    scope :v1 do
      mount_devise_token_auth_for "User", at: 'auth'
    end
  end
end

我的用户模型:

class User < ActiveRecord::Base
  # Include default devise modules.
  devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable,
          :confirmable, :omniauthable
  include DeviseTokenAuth::Concerns::User
end

1 个答案:

答案 0 :(得分:0)

你可以做的一些事情:

  1. 为您的路线添加约束,以便它们只提供JSON(您已经做过)IE:
  2. AD\

    1. 在应用程序控制器中添加自定义异常处理程序: constraints: { format: :json }