railstutorial.org - Rails 教程 user.save 给出参数错误( 1 代表 0 )

  显示原文与译文双语对照的内容
0 0

首先,在这里发布一些 noob,所以我知道如果我的格式是关闭的或者什么样的,我很高兴修复。

我一直在跟随 Michael Rails的hartl教程。 我相信我的问题是因为我没有做正确的事,但是我在网上找不到什么能解决我的问题。

我在第 6章创建一个用户数据库。 在我尝试做 user.save 之前,一切都正常

仅供参考,下面是导致问题的步骤:


rails generate model User name:string email:string

然后:


bundle exec rake db:migrate 

打开沙盒控制台:


 rails console --sandbox

创建新用户


user = User.new(name:"Michael Hartl", email:"mhartl@example.com")

保存它:

 
user.save

 

所有的东西都给了我预期的输出,直到 user.save

irb ( 主要): 002: 0> user.save ( 0.1 ms ) 保存点 active_record_1 ( 0.1 ms ) 回滚至保存保存 active_record_1 ArgumentError: in ( 1,0 )/abstract_adapter 。rb: 271中的参数数目错误: initialize' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adap ters/abstract_adapter.rb:271:in 从/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adap 发送器/abstract_adapter 。cer: 271中新建: substitute_at' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:97: in substitute_values中的块,从/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:96: 中 each' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:96: in each_with_index来自/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:96: substitute_values' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:56: in 插入'/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb: 521: _create_record' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/counter_cache.r b:139:in _create_record:/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/attribute_metho ds/dirty 。rb: 122 _create_record' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:30 6:in 在_create_record中块,从/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb: 88: call' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb: 88:in _run_callbacks'/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb: 734: _run_create_callbacks' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:30 6:in _create_record'。22级别。来自/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/railties-4.2.0.beta4/lib/rails/commands/console.rb:9:in s tart' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/railties-4.2.0.beta4/lib/rails/commands/commands_tasks.rb: 68:in 控制台'/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/railties-4.2.0.beta4/lib/rails/commands/commands_tasks.rb: 39: run_command!' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/railties-4.2.0.beta4/lib/rails/commands.rb:17:in '/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.: 252: require' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies. rb:252:in block/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.: 237中的块 load_dependency' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies. rb:252:in 要求来自/Users/Jonathon/Documents/Aptana Studio 3 Workspace/RoR/bin/rails:8:in <top (required)>' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies. rb:246:in 从/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.: 246中加载 block in load' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies. rb:237:in load_dependency'/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.: 246 load' from/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in 要求来自/Users/Jonathon/. rbenv/versions/2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require' from -e:1:in '( 主要): 003: 0>

我已经将 user.rb 文件放在这里:

class User <ActiveRecord::Base end

我的create_users.rb 文件也在这里

class CreateUsers <ActiveRecord::Migration def change create_table :users do |t| t.string :name t.string :email t.timestamps null: false end end end

谢谢并让我知道,如果有更多的信息我需要提供 !

时间: 原作者:

0 0

感谢你的帮助 !

Slothbear是对的,最终是我的数据库出现问题。 克隆了教程git并解决了问题。

原作者:
0 0

我也有同样的问题。 首先我在我尝试了 user.save的时候就有了 ArgumentError 。 当我尝试 rake db:drop 然后 rake db:migrate 时,也遇到同样的错误。

我的例子是 bundle update 之后,我发现这个 arel gem 版本使得这个问题变得。 它在 Gemfile.lock 上显示 arel (6.0.0),但当我像以前一样尝试 arel (6.0.0.beta2) 时,它。

我不知道我找到了的讨论。 现在我们没有非 beta Rails 4.2.0的问题,如果我们在示例应用程序上没有 bundle update,那么Hartl教程也没问题。

原作者:
...