Ruby on rails 4 Rails 4和Desive 3.2:已完成401未经授权的错误

Ruby on rails 4 Rails 4和Desive 3.2:已完成401未经授权的错误,ruby-on-rails-4,devise,Ruby On Rails 4,Devise,当我试图运行以下命令的 curl -u a@aa.com:a http://localhost:3002/home/test_json.json 及 我得到了如下回应 {"error":"You need to sign in or sign up before continuing."} 状态为401未经授权。 我不知道为什么会出现这个错误,即使我提供了用户电子邮件和密码与这些命令 下面是我的代码 应用程序控制器 class ApplicationController < Actio

当我试图运行以下命令的

curl -u a@aa.com:a http://localhost:3002/home/test_json.json

我得到了如下回应

{"error":"You need to sign in or sign up before continuing."}
状态为401未经授权。

我不知道为什么会出现这个错误,即使我提供了用户电子邮件和密码与这些命令

下面是我的代码

应用程序控制器

class ApplicationController < ActionController::Base

  #skip_before_filter  :verify_authenticity_token
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.


  before_filter :configure_permitted_parameters, if: :devise_controller?

    protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :authentication_token) }
      devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :authentication_token) }
    end

    protect_from_forgery with: :exception

  end
class HomeController < ApplicationController
  #skip_before_filter  :verify_authenticity_token
  before_filter :authenticate_user!#, except: [:test_json]

  def index
  end

  def test_json
   msg = {success: true, data: 'abcd' }
   render json: msg
  end

end
routes.rb

DeviseTest::Application.routes.draw do

  devise_for :users

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root "home#index"

  get 'home/test_json' => 'home#test_json'


end
对不起,我的英语和语法,请帮忙


提前谢谢。

你有没有找到导致这种情况的原因?是的,我得到了答案,答案是什么?
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable
end
# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|

  # ==> Mailer Configuration
  # Configure the e-mail address which will be shown in Devise::Mailer,
  # note that it will be overwritten if you use your own mailer class
  # with default "from" parameter.
  config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'

  # Configure the class responsible to send e-mails.
  # config.mailer = 'Devise::Mailer'

  # ==> ORM configuration
  # Load and configure the ORM. Supports :active_record (default) and
  # :mongoid (bson_ext recommended) by default. Other ORMs may be
  # available as additional gems.
  require 'devise/orm/active_record'

  # Configure which authentication keys should be case-insensitive.
  # These keys will be downcased upon creating or modifying a user and when used
  # to authenticate or find a user. Default is :email.
  config.case_insensitive_keys = [ :email ]

  # Configure which authentication keys should have whitespace stripped.
  # These keys will have whitespace before and after removed upon creating or
  # modifying a user and when used to authenticate or find a user. Default is :email.
  config.strip_whitespace_keys = [ :email ]

  # By default Devise will store the user in session. You can skip storage for
  # particular strategies by setting this option.
  # Notice that if you are skipping storage for all authentication paths, you
  # may want to disable generating routes to Devise's sessions controller by
  # passing :skip => :sessions to `devise_for` in your config/routes.rb
  config.skip_session_storage = [:http_auth]

  # ==> Configuration for :database_authenticatable
  # For bcrypt, this is the cost for hashing the password and defaults to 10. If
  # using other encryptors, it sets how many times you want the password re-encrypted.
  #
  # Limiting the stretches to just one in testing will increase the performance of
  # your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use
  # a value less than 10 in other environments.
  config.stretches = Rails.env.test? ? 1 : 10

  # If true, requires any email changes to be confirmed (exactly the same way as
  # initial account confirmation) to be applied. Requires additional unconfirmed_email
  # db field (see migrations). Until confirmed new email is stored in
  # unconfirmed email column, and copied to email column on successful confirmation.
  config.reconfirmable = true

  # ==> Configuration for :validatable
  # Range for password length.
  config.password_length = 1..128

  # Time interval you can reset your password with a reset password key.
  # Don't put a too small interval or your users won't have the time to
  # change their passwords.
  config.reset_password_within = 6.hours

  config.secret_key = '1a06c85c7aaaf467562cbeb150f152b44c7a26de252fca87a1db604489bfaa4b40da4f06f8a9e59f150ee0d6e07ace86aab33d5ec8a159fdbe652ffa8745e4dc'

end
DeviseTest::Application.routes.draw do

  devise_for :users

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root "home#index"

  get 'home/test_json' => 'home#test_json'


end