开发者

rails3 model redirect

开发者 https://www.devze.com 2023-04-06 20:39 出处:网络
I have a model called User. In the model I want to check if a value is true or false. If the value is true then break all operations and redirect to a specific page.

I have a model called User. In the model I want to check if a value is true or false. If the value is true then break all operations and redirect to a specific page.

How do I do that?

before_create :check_user

def check_user
    if User.find_by_email(self.email)
        redirect_to root_开发者_开发技巧path
    end
end


You can't and you never need to redirect form model.

You should do it in controller something like

before_filter :check_user
...
private
def check_user
  redirect_to root_path unless User.find_by_email(params[:user][:email])
end


before_create :check_user

def check_user
    unless User.find_by_email(email).nil?
        redirect_to root_path
    end
end

See if this does what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消