本文介绍了使用params hash的内容手动实例化Devise用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚我的想法是

I'm losing my mind trying to figure this out:

有一个注册模块可以避开主要的devise注册控制器,我必须手动实例化使用User.new(...)创建由Devise生成的用户模型。我正在使用form_tag提交User.new()所需的所有必要参数。这是我的代码的简化版本:

There's a registration module which side-steps the main devise registrations controller, and I have to manually instantiate the Devise-generated user model using User.new(...). I'm using a form_tag to submit all of the necessary parameters necessary for User.new(). Here's a truncated version of my code:

def registration_three

  @user = User.new(:email => params[:email], :password => params[:password], :password_confirmation => params[:password_confirmation])
  @user.save

  @user = User.new(:email => '[email protected]', :password => 'qwertyui', :password_confirmation => 'qwertyui')
  @user.save

end

第二个@ user.save有效,但第一个无效。根据stdout中的print语句,params [:email],params [:password]和params [:password_confirmation]当然存在。

The second @user.save works, but the first one doesn't. params[:email], params[:password], and params[:password_confirmation] certainly exist, according to the print statements in stdout.

有人有什么想法吗?

编辑:
因此,我尝试将第二个User.new(...)中的所有单引号字符串更改为双引号字符串,例如那些存储在params哈希中的代码...第二个实例化也失败了。因此,我相当确定这个问题涉及双引号和单引号字符串。有人知道如何将双引号字符串转换为单引号字符串吗?提前致谢。

So I tried changing all of the single-quote strings in the second User.new(...) to double-quote strings like the ones stored in the params hash...and the second instantiation failed as well. So I'm fairly sure that the issue involves double- vs single-quote strings. Anyone know how to convert double-quote strings to single-quote ones? Thanks in advance.

推荐答案

首先将参数存储在变量中,然后在查询中使用它们以避免引用错误

Store your params in variable first then use them in query to avoid quote errors

这篇关于使用params hash的内容手动实例化Devise用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 17:17