[英]Sinatra: wrong number of arguments (4 for 0..2)
I'm currently developing an app with Sinatra , ActiveRecord and MySQL . 我目前正在使用Sinatra , ActiveRecord和MySQL开发一个应用程序。 I'm working on the sign up form, which looks like this:
我正在使用注册表单,看起来像这样:
app.rb : app.rb :
post '/signup' do
password_salt = BCrypt::Engine.generate_salt
password_hash = BCrypt::Engine.hash_secret(params[:password], password_salt)
@usuarios = User.new(params[:nombre], params[:cedula], password_hash, "admin")
if @usuarios.save
redirect './signup', :notice => "Usuario creado exitosamente."
else
redirect './signup', :error => "Ha ocurrido un error, intente nuevamente."
end
end
And the view looks like this, signup.erb : 看起来像这样signup.erb :
<form id="registro" action="/signup" method="POST">
<fieldset>
<legend>Ingrese sus datos</legend>
<label>Nombre
<input type="text" name="nombre">
</label>
<label>Cédula
<input type="text" maxlength="10" name="cedula">
</label>
<label>Contraseña
<input type="password" name="password">
</label>
<!-- TO-DO:
Dropdown list con los diferentes tipos de usuarios, i.e.: admin, secretario, etc.
-->
<input type="submit" id="registerButton" class="button small">Finalizar registro</a>
</fieldset>
</form>
Whenever I try to create a new user, I get the following error: 每当我尝试创建新用户时,都会出现以下错误:
ArgumentError - wrong number of arguments (4 for 0..2)
Considering that the table I'm trying to insert the values has 4 columns, I don't understand why I'm getting this error. 考虑到我要插入值的表有4列,我不明白为什么会收到此错误。
Any insight to help me solve this inconvenience would be greatly appreciated! 任何能帮助我解决此不便的见解将不胜感激!
Thanks in advance. 提前致谢。
ActiveRecord::new
method allows only 2 parameters as arguments, it should be a hash. ActiveRecord::new
方法仅允许2个参数作为参数,它应该是一个哈希。 fix: 固定:
User.new(params[:nombre], params[:cedula], password_hash, "admin")
to: 至:
User.new(nombre: params[:nombre], cedula: params[:cedula], password: password_hash, role: "admin")
You should always check the documentation, in 99% cases you can find a problem: 您应该始终检查文档,在99%的情况下,您会发现问题:
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names) .
新对象可以实例化为空(不传递任何构造参数)或具有属性但尚未保存的预置(尚未传递的键名称与关联的表列名称匹配的哈希值) 。 In both instances, valid attribute keys are determined by the column names of the associated table – hence you can't have attributes that aren't part of the table columns.
在这两种情况下,有效的属性键均由关联表的列名确定–因此,您不能拥有不属于表列的属性。
new(attributes = nil, options = {})
Examples: 例子:
# Instantiates a single new object
User.new(:first_name => 'Jamie')
# Instantiates a single new object using the :admin mass-assignment security role
User.new({ :first_name => 'Jamie', :is_admin => true }, :as => :admin)
# Instantiates a single new object bypassing mass-assignment security
User.new({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.