before_validation方法不起作用

时间:2015-03-18 04:26:48

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

我是rails中的newB,我正在开发一个项目,并希望从用户名和电子邮件中删除尾随和前导空格。所以我在用户模型中创建了一个方法

class User < ActiveRecord::Base

    include CarrierWave::MiniMagick

    #removes the white spaces before validating the data 


    before_validation :strip_whitespace, :only => [:name,:email]

    #data validations
    validates :email, :presence =>true,  :uniqueness => {case_sensitive: false}, :format => { :with=> /([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/, :message => "please enter a valid e-mail" }
    validates :name, :presence=>true
    validates :password ,:presence =>true, :confirmation=> true #, :length =>{ :minimum=>6, :maximum=>30}, :format=>{:with=>/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,30}/}

    #for the image
    mount_uploader :image, ImageUploader

    #for the password
    has_secure_password


    #associations
    has_many :issues
    has_many :comments

end



def strip_whitespace
    self.email = self.email.squish
    self.name = self.name.squish
end

当我在Users.create操作中输入用户信息时,前导和尾随空格被删除,但是当我从会话控制器登录时,前导和尾随空格不会被删除,因此它显示错误。 请帮忙

用户控制器的代码

class UsersController < ApplicationController
before_action :not_logged_in?, :only => [:new]  

  def new
    @user=User.new
  end

  def create
  @user = User.new(user_params)
    if @user.save 
      sign_in @user
      RegistrationsMailer.welcome_email(@user).deliver
        flash[:success] = 'you are successfully registered'
        redirect_to :controller => 'users' , :action => 'show', :id => @user.id
     else
        render 'new'
     end 
  end


  def show
    @user=User.find(params[:id])
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      redirect_to @user
    else 
      render 'edit'
    end
  end


protected
    def user_params
    params.require(:user).permit(:name,:email,:password,:image) 

    end

end

会话控制器的代码

class SessionsController < ApplicationController

    def new


    end


    def create
        @user = User.find_by_email(params[:sessions][:email])
        if @user && @user.authenticate(params[:sessions][:password])
            sign_in @user
            redirect_to @user
        else 
            flash.now[:error] = 'Invalid email or password'
            render 'new'
        end

    end



    def destroy
    end


end

请帮忙

1 个答案:

答案 0 :(得分:1)

根据When does Validation happen?

  

以下方法触发验证,并且仅当对象有效时才将对象保存到数据库:

     

创建

     

创建!

     

保存

     

保存!

     

更新

     

更新!

当您创建或销毁新会话时,您实际上并未在用户模型上调用任何这些方法,因此无法调用验证。

但是由于你的数据库有压扁值,你需要在SessionsController中修改你的创建动作来压缩传递的参数。

def create
    @user = User.find_by_email(params[:sessions][:email].squish)
    if @user && @user.authenticate(params[:sessions][:password].squish)
        sign_in @user
        redirect_to @user
    else 
        flash.now[:error] = 'Invalid email or password'
        render 'new'
    end
end