简体   繁体   English

Sinatra:参数数量错误(0..2为4)

[英]Sinatra: wrong number of arguments (4 for 0..2)

I'm currently developing an app with Sinatra , ActiveRecord and MySQL . 我目前正在使用SinatraActiveRecordMySQL开发一个应用程序。 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.

相关问题 “参数数量错误(2为1)(ArgumentError)”的问题 - Problems with “wrong number of arguments (2 for 1) (ArgumentError)” Ruby MySQL错误的参数数量 - ruby mysql wrong number of arguments ProgrammingError:字符串格式化过程中的参数数量错误 - ProgrammingError: Wrong number of arguments during string formatting Rake不会回滚迁移,参数数量错误(1代表0) - Rake won't rollback migrations, wrong number of arguments (1 for 0) 编程错误:字符串格式化期间的参数数量错误,尝试过元组修复 - ProgrammingError: Wrong number of arguments during string formatting, tried tuple fix Angular,Flask,mysql.connector编程错误:参数数量错误 - Angular, Flask, mysql.connector ProgrammingError: Wrong number of arguments 在ruby中连接到mysql会产生错误数量的参数错误(4之0)-如何调试? - Connecting to mysql in ruby produces wrong number of arguments error (4 of 0) - how to debug? ArgumentError(错误的参数数量(2为1))-RoR应用程序的“查找”和“创建” - ArgumentError (wrong number of arguments (2 for 1)) - `find' and `create' for RoR app mysql.connector.errors.ProgrammingError:字符串格式化期间参数数量错误 - mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting 无法将Scrapy项目用作MySQL.execute的数据:“字符串格式化期间参数数量错误” - Unable to use Scrapy item as data for MySQL.execute: “Wrong number of arguments during string formatting”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM