新手问题 devise:即使填入数据,也一直提示 “不能为空”

5night · 2013年10月25日 · 最后由 5night 回复于 2013年10月25日 · 3856 次阅读

一直提醒“用户名 不能为空字符,Signature 不能为空字符” 去掉 presence: true 后数据库中仍旧为空,遍查 stackoverflow,发现两个:

http://stackoverflow.com/questions/10907947/devise-gives-me-an-email-cannot-be-blank-error-even-though-email-is-not-blank

http://stackoverflow.com/questions/17256496/why-does-devise-keep-asking-me-that-the-name-cant-be-blank 情况也不相同,其中有一个似乎到最后都没有解决。。。

view:

<div class="control-group">
    <label class="control-label">用户名</label>

    <div class="controls">
        <%= f.input :nickname, label: false %>
        <div class="hint">请使用字母和数字,4位至10位</div>
    </div>
</div>

<div class="control-group">
    <label class="control-label">签名</label>

    <div class="controls">
        <%= f.input :signature, label: false %>
        <div class="hint">test</div>
    </div>
</div>

<div class="control-group">
    <label class="control-label">E-mail</label>
    <div class="controls">
        <%= f.input :email, label: false %>
        <div class="hint">请输入常用E-mail,便于登录和找回密码</div>
    </div>
</div>

debug:

--- !ruby/hash:ActionController::Parameters
utf8: 
authenticity_token: et2G/Hl8K8qIdfJh6qszhG/bTotFDSnDpPqJMPHY8Zg=
member: !ruby/hash:ActionController::Parameters
  nickname: lalala
  signature: lalala
  email: 1@2.com
  password: lalala
  password_confirmation: lalala
commit: 注册
action: create
controller: devise/registrations

member.rb

class Member < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable


  VALID_NICKNAME_REGEX = /\A[a-zA-Z\d]+/

  validates :nickname,  presence: true  

  # ,
  # format: { with: VALID_NICKNAME_REGEX }, 
  # length: { minimum: 4,maximum: 10 },
  # uniqueness:  { case_sensitive: false, message: "已被注册" }

  validates :signature,  presence: true  

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
  validates :email, presence: true,
  format: { with: VALID_EMAIL_REGEX },
  uniqueness:  { case_sensitive: false, message: "已被注册" }


  validates :password, length: { minimum: 6 }
  mount_uploader :avatar, AvatarUploader

  STU_NUM_REGEX= /\A(\d[0-2][\d]{4}[2,9]\d{3})\z/
  validates :student_number, length: { minimum: 10,maximum: 10 },
  format: { with: STU_NUM_REGEX }, allow_blank: true

end

rails 4

class Users::RegistrationsController < Devise::RegistrationsController

  before_filter :update_sanitized_params, if: :devise_controller?

  def update_sanitized_params
     devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:nickname, :email, :password, :password_confirmation)}
  end

end

谢谢这么快的回复~ 先用手机回复一下,明天起来仔细研究~

解决方法:

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
  before_filter :configure_permitted_parameters, :only => [:create]

  protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:nickname, :email, :password) }
    end
end
#config/routes.rb
devise_for :members, :controllers => { :registrations => "registrations" }
需要 登录 后方可回复, 如果你还没有账号请 注册新账号