Ruby on rails activerecord始终存在并返回false

Ruby on rails activerecord始终存在并返回false,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在检查数据库中是否存在记录,但即使它确实存在,也会不断返回false。我在rails的控制台上也尝试过,它返回true,但在我的应用程序中它不起作用,我似乎找不到原因? 更新(完整代码): 我手动检查了参数,它返回了正确的值,我也检查了我的数据库,它是正确的,但是当我使用exists时,它一直返回false?方法 更新(控制台日志): 你应该这样做: def create if params[:loginbtn] if @account = Account.find_by(use

我正在检查数据库中是否存在记录,但即使它确实存在,也会不断返回false。我在rails的控制台上也尝试过,它返回true,但在我的应用程序中它不起作用,我似乎找不到原因? 更新(完整代码):

我手动检查了参数,它返回了正确的值,我也检查了我的数据库,它是正确的,但是当我使用exists时,它一直返回false?方法

更新(控制台日志):


你应该这样做:

def create
  if params[:loginbtn]
    if @account = Account.find_by(username: params[:account][:username])
      session[:username]  = @account.username
      session[:logged]    = true
      redirect_to root_path, notice: "Hello, #{@account.username}! You successfully logged in!" and return
    else
      redirect_to root_path, notice: "Invalid username or password. Create a new account ?" and return
    end
  else
    @account = Account.new(account_params)
    if @account.save?
      session[:username]  = @account.username
      session[:logged]    = true
      redirect_to root_path, notice: "Hello, #{@account.username}! You successfully created your account!" and return
    else
      redirect_to root_path, notice: "Username already taken !" and return
    end
  end
  render 'index'  
end
在登录的情况下,如果
@account
为nil(即找不到/不存在),那么您将使用else语句

在register的情况下,如果
@account.save
失败(即,由于用户名已被占用而无效),那么您也会使用else语句

此外,我不确定您是否需要:

elsif params[:registerbtn]
除非有其他方式提交表格。所以可能只是
else

帕万的建议很好。在这种情况下,这可能成为:

def create
  if @account = Account.find_or_create_by(account_params)
    session[:username]  = @account.username
    session[:logged]    = true
    redirect_to root_path, notice: "Hello, #{@account.username}! You successfully logged in!" and return
  else
    redirect_to root_path, notice: "Invalid username or password. Create a new account ?" and return
  end
  render 'index'  
end

你应该这样做:

def create
  if params[:loginbtn]
    if @account = Account.find_by(username: params[:account][:username])
      session[:username]  = @account.username
      session[:logged]    = true
      redirect_to root_path, notice: "Hello, #{@account.username}! You successfully logged in!" and return
    else
      redirect_to root_path, notice: "Invalid username or password. Create a new account ?" and return
    end
  else
    @account = Account.new(account_params)
    if @account.save?
      session[:username]  = @account.username
      session[:logged]    = true
      redirect_to root_path, notice: "Hello, #{@account.username}! You successfully created your account!" and return
    else
      redirect_to root_path, notice: "Username already taken !" and return
    end
  end
  render 'index'  
end
在登录的情况下,如果
@account
为nil(即找不到/不存在),那么您将使用else语句

在register的情况下,如果
@account.save
失败(即,由于用户名已被占用而无效),那么您也会使用else语句

此外,我不确定您是否需要:

elsif params[:registerbtn]
除非有其他方式提交表格。所以可能只是
else

帕万的建议很好。在这种情况下,这可能成为:

def create
  if @account = Account.find_or_create_by(account_params)
    session[:username]  = @account.username
    session[:logged]    = true
    redirect_to root_path, notice: "Hello, #{@account.username}! You successfully logged in!" and return
  else
    redirect_to root_path, notice: "Invalid username or password. Create a new account ?" and return
  end
  render 'index'  
end
我手动检查参数,它返回正确的值,我也 检查我的数据库它是正确的,但当我使用 存在?方法

该方法要求整数/字符串/数组/哈希作为
参数
,但您正在传递
帐户参数
,它是
ActionController::Parameters
的实例。因此,它总是返回
false

它似乎最适合你正在尝试做的事情

我手动检查参数,它返回正确的值,我也 检查我的数据库它是正确的,但当我使用 存在?方法

该方法要求整数/字符串/数组/哈希作为
参数
,但您正在传递
帐户参数
,它是
ActionController::Parameters
的实例。因此,它总是返回
false



它似乎最适合您正在尝试的操作

hi@Pavan,我还尝试给params[:username]一个字符串,但它不起作用。我甚至试图给出一个具有正确用户名的字符串,但仍然返回false@alexeim您应该试试
params[:account][:username]
而不是。是的,我不确定传递
ActionController::Parameters的实例是否是问题所在。对
ActionController::Parameters
的实例调用
是一个?(散列)
返回
true
。即使在调试时,我也会在if语句前面使用put,并返回正确的字符串,然而,当它输入if语句时,它是false。@treiff在这里对我来说与
4.2.3
相同,但在
5.x
中,我得到了
false
hi@Pavan,我还试图给params[:username]一个字符串,但它不起作用。我甚至试图给出一个具有正确用户名的字符串,但仍然返回false@alexeim您应该试试
params[:account][:username]
而不是。是的,我不确定传递
ActionController::Parameters的实例是否是问题所在。对
ActionController::Parameters
的实例调用
是一个?(散列)
返回
true
。即使在调试时,我也会在if语句前面使用put,并返回正确的字符串,然而,当它输入if语句时,它是假的。@treiff在这里对我来说与
4.2.3
相同,但在
5.x
中,我得到了
false
hi@jvillian,如果没有活动会话但没有记录,默认情况下会创建一个新帐户。我试着按你的方式做,但似乎出现了语法错误,我不知道如何修复:SyntaxError(/mnt/c/Sites/formtest/app/controllers/formulaire\u controller.rb:9:语法错误,意外的tLABEL,需要关键字\u then或“;”或“\n”nt=Account.find\u by username:params[:username]尝试将那些
find_by
参数放在括号中(请参见更新的答案)。我认为您需要
如果params[:commit]=='loginbtn'
,按钮的值将在
params[:commit]中传递
@treiff-好主意!alexeim-也许可以添加你的控制台日志-特别是参数-你就会看到treiff在说什么。两件事:(1)帐户存在?(帐户参数)
在我看来,非常不习惯,我不推荐它,以及(2)你仍然应该考虑Pavan的建议,因为它是,IMO,更干净的代码。但是,嗨,JVILIN,一个新的帐号是默认的,如果没有激活的会话,但是它没有被记录。我试着用你的方式做它,但是看起来好像有一个语法错误,我不知道如何修复它:SycRead错误。(/mnt/c/Sites/formtest/app/controllers/formulaire_controller.rb:9:语法错误,意外的tLABEL,应为关键字_then或“;”或“\n”nt=Account.find_by username=='loginbtn'
,按钮的值将在
params[:commit]
@treiff-很好的捕获!alexeim-也许添加你的控制台日志-特别是params-你会看到treiff在说什么。两件事:(1)Account.exists?(Account\u params)在我看来,非常不习惯,我不推荐它,还有(2)你仍然应该考虑Pavan的建议,因为它是,IMO,更干净的代码。但是,很高兴它工作!
def create
  if @account = Account.find_or_create_by(account_params)
    session[:username]  = @account.username
    session[:logged]    = true
    redirect_to root_path, notice: "Hello, #{@account.username}! You successfully logged in!" and return
  else
    redirect_to root_path, notice: "Invalid username or password. Create a new account ?" and return
  end
  render 'index'  
end