Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在下面的if语句(Rails)中检查电子邮件是否已被接收?_Ruby On Rails_Ruby_If Statement_Omniauth - Fatal编程技术网

Ruby on rails 在下面的if语句(Rails)中检查电子邮件是否已被接收?

Ruby on rails 在下面的if语句(Rails)中检查电子邮件是否已被接收?,ruby-on-rails,ruby,if-statement,omniauth,Ruby On Rails,Ruby,If Statement,Omniauth,我有以下行动: users.rb: 它做了以下工作: 如果用户的电子邮件不是空的,请给他登录,并将他重定向到他的个人资料,如果他的id为零,请保存他。换句话说,如果他还没有被创造出来。 如果用户的电子邮件为空,请发送给他以输入用户可以输入其电子邮件的路径。 现在我想添加另一个if语句,如果电子邮件已经被接收,它会闪烁一个错误,并将用户重定向到根路径 我不知道怎么做,有什么建议吗?那么把if语句放在哪里呢 编辑: 奇怪的是,得到的不是重定向到根路径: Validation failed: Emai

我有以下行动:

users.rb:

它做了以下工作:

如果用户的电子邮件不是空的,请给他登录,并将他重定向到他的个人资料,如果他的id为零,请保存他。换句话说,如果他还没有被创造出来。 如果用户的电子邮件为空,请发送给他以输入用户可以输入其电子邮件的路径。 现在我想添加另一个if语句,如果电子邮件已经被接收,它会闪烁一个错误,并将用户重定向到根路径

我不知道怎么做,有什么建议吗?那么把if语句放在哪里呢

编辑:

奇怪的是,得到的不是重定向到根路径:

Validation failed: Email has already been taken
我不知道这是否有帮助,但以下是from_omniauth的来源:

当前的代码:

user.rb:

其余的没有改变


代码似乎忽略了if User.exists?:email=>User.email部分?

不确定您想要的是rails答案还是SQL答案,但您可以使用以下SQL来查找您的响应:

select id from users where email = :email 
如果返回0行,则电子邮件不存在;如果返回1,则电子邮件确实存在。我想你也可以用

Users#find(:first, :conditions => 'email = #{email}') 

但是我还没有对此进行测试。

不确定您想要的是rails答案还是SQL答案,但是您可以使用以下SQL来查找您的响应:

select id from users where email = :email 
如果返回0行,则电子邮件不存在;如果返回1,则电子邮件确实存在。我想你也可以用

Users#find(:first, :conditions => 'email = #{email}') 

但是我还没有对此进行测试。

Rails有一种基于参数检查对象是否正确的方法。您可以这样做:

if user.email.present?
  if user.id.nil?
    if User.exists?(:email => user.email)
      # return redirect email is already token
    end

    # save user
  end
  # sign_in user
else
  # redirect to get email
end
顺便说一下,我不熟悉Omniauth,所以我不确定什么是正确的,但通常用于检查对象是否已保存。如果你有身份证,通常是。 如果您感到困惑,您可以在您的用户模型中创建函数,以便更好地阅读

class User
  def new?
    id.nil?
  end

  def email_taken?
    self.class.exists?(:email => email)
  end
end

# back to controller
if user.email.present?
  if user.new?
    if user.email_taken?
      # return redirect email is already token
    end

    # save user
  end
  # sign_in user
else
  # redirect to get email
end

Rails有一种基于参数检查对象是否正确的方法。您可以这样做:

if user.email.present?
  if user.id.nil?
    if User.exists?(:email => user.email)
      # return redirect email is already token
    end

    # save user
  end
  # sign_in user
else
  # redirect to get email
end
顺便说一下,我不熟悉Omniauth,所以我不确定什么是正确的,但通常用于检查对象是否已保存。如果你有身份证,通常是。 如果您感到困惑,您可以在您的用户模型中创建函数,以便更好地阅读

class User
  def new?
    id.nil?
  end

  def email_taken?
    self.class.exists?(:email => email)
  end
end

# back to controller
if user.email.present?
  if user.new?
    if user.email_taken?
      # return redirect email is already token
    end

    # save user
  end
  # sign_in user
else
  # redirect to get email
end
试试这个

 def omniauth_create
    auth = request.env["omniauth.auth"]
    user = User.from_omniauth(env["omniauth.auth"])
    if user.email.present?
      if user.id.nil?
        if User.find_by_email(user.email).present?
          # send error email already be taken
          # or login with that user that means define that user for sign in
        else
          # save user and login with that user
          user.save!  
        end
        sign_in user
        redirect_back_or user
      end 
    else
      # Send user to a form to fill his email
      # session[:omniauth] = request.env['omniauth.auth'].except('extra')
      redirect_to(enter_email_path(oprovider: user.provider,
                                   ouid: user.uid,
                                   oname: user.name,
                                   opassword: user.password,
                                   opassword_confirmation: user.password))

    end
更新

您也可以使用该方法

更新2

在模态文件中

  class << self
    def create_with_omniauth(auth)
      create! do |user|
        user.provider = auth['provider']
        user.uid = auth['uid']
        if auth['info']
          user.uid = auth['uid'] || ""
          user.name = auth['info']['name'] || ""
          user.email = auth['info']['email'] || ""
          user.access_token = auth['credentials']['token'] || ""
          user.oauth_token_secret = auth['credentials']['secret'] || ""
          user.oauth_token = auth['credentials']['token'] || ""
        end
      end
    end
  end
试试这个

 def omniauth_create
    auth = request.env["omniauth.auth"]
    user = User.from_omniauth(env["omniauth.auth"])
    if user.email.present?
      if user.id.nil?
        if User.find_by_email(user.email).present?
          # send error email already be taken
          # or login with that user that means define that user for sign in
        else
          # save user and login with that user
          user.save!  
        end
        sign_in user
        redirect_back_or user
      end 
    else
      # Send user to a form to fill his email
      # session[:omniauth] = request.env['omniauth.auth'].except('extra')
      redirect_to(enter_email_path(oprovider: user.provider,
                                   ouid: user.uid,
                                   oname: user.name,
                                   opassword: user.password,
                                   opassword_confirmation: user.password))

    end
更新

您也可以使用该方法

更新2

在模态文件中

  class << self
    def create_with_omniauth(auth)
      create! do |user|
        user.provider = auth['provider']
        user.uid = auth['uid']
        if auth['info']
          user.uid = auth['uid'] || ""
          user.name = auth['info']['name'] || ""
          user.email = auth['info']['email'] || ""
          user.access_token = auth['credentials']['token'] || ""
          user.oauth_token_secret = auth['credentials']['secret'] || ""
          user.oauth_token = auth['credentials']['token'] || ""
        end
      end
    end
  end

答案是。我想把它放在控制器中,但是if语句让我感到困惑。它不应该是用户吗。find:first,:conditions=>'email={user.email}'?对不起,如果我弄错了,请回答。我想把它放在控制器中,但是if语句让我感到困惑。它不应该是用户吗。find:first,:conditions=>'email={user.email}'?对不起,如果我弄错了。非常感谢!但我的验证失败:电子邮件已被接收,请查看我的编辑。@alexchenco收到电子邮件后我没有写回信吗?随机的。您想退出函数,但不想保存此用户。所以你应该在这里使用return-redirect\u-to-root\u路径。@redirect\u-back\u或user噢,对不起,没有看到。我忘了return在Ruby中做了什么。谢谢顺便问一下,为什么在redirect_back_或user中不需要return?@alexchenco,因为这是函数的结尾,之后不会执行其他代码。你可以添加它,如果你想,但这不会改变任何事情。在ruby中,函数执行的最后一条语句是自动返回的。哦,我明白了,我把结尾看作if的结尾。没有把其他的当做结束。再次感谢!谢谢!但我的验证失败:电子邮件已被接收,请查看我的编辑。@alexchenco收到电子邮件后我没有写回信吗?随机的。您想退出函数,但不想保存此用户。所以你应该在这里使用return-redirect\u-to-root\u路径。@redirect\u-back\u或user噢,对不起,没有看到。我忘了return在Ruby中做了什么。谢谢顺便问一下,为什么在redirect_back_或user中不需要return?@alexchenco,因为这是函数的结尾,之后不会执行其他代码。你可以添加它,如果你想,但这不会改变任何事情。在ruby中,函数执行的最后一条语句是自动返回的。哦,我明白了,我把结尾看作if的结尾。没有把其他的当做结束。再次感谢!谢谢但得到这个:验证失败:电子邮件已被采取,请查看我的编辑。谢谢!但我很困惑,你是想把它放在我的创建动作还是omniauth_创建动作中?编辑:但是如果电子邮件和密码是空的,那么我会得到一个错误?是的,我得到了这个:验证失败:电子邮件不能为空,电子邮件无效,密码确认不能为空:谢谢!但得到这个:验证失败:电子邮件已被采取,请查看我的编辑。谢谢!但我很困惑,你是想把它放在我的创建动作还是omniauth_创建动作中?编辑:但如果电子邮件和密码是空的,那么我会得到一个 错误?是的,我得到了:验证失败:电子邮件不能为空,电子邮件无效,密码确认不能为空: