Ruby 2.0 + Rails 4 +设计3.1.1注销问题

时间:2013-10-10 21:37:52

标签: ruby devise ruby-on-rails-4

如果我点击退出按钮,我会收到此错误:检查结果必须仅为ASCII或使用与默认外部相同的编码

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.

  before_action :authenticate_user!
  before_action :configure_permitted_parameters, if: :devise_controller?

  protect_from_forgery with: :exception

  protected

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

end

链接:

<%= link_to "Logout", logout_path, title: "Logout", method: :delete %>

路线:

devise_scope :user do
  get 'register', to: 'devise/registrations#new', as: 'register'
  get 'login', to: 'devise/sessions#new', as: 'login'
  delete 'logout', to: 'devise/sessions#destroy', as: 'logout'
end

我不知道问题出在哪里。

如果我删除这3行,那么它可以工作,但我无法注册新用户:

devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试使用以下

devise_parameter_sanitizer.for(:sign_up) << :username

如果您还需要另一个字段,请添加如上所示的行

答案 1 :(得分:0)

我不确定这是否是原因,但我注意到您在登录时不允许将密码列入白名单。我认为您需要这样做,因为您在注册和帐户更新时需要输入密码。看起来应该是这样的

devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email, :password) }