Rails教程user.save给出参数错误(1表示0)

时间:2014-12-14 01:40:26

标签: ruby-on-rails railstutorial.org

首先,在这里张贴一个菜鸟的位置,所以请告诉我,如果我的格式是关闭的或类似的东西,我会很乐意解决它:)

我一直在关注迈克尔·哈特尔的Rails教程,它很棒。我确定我的问题是由于我没有做正确的事情,但我无法在网上找到可以解决问题的任何内容。

我在第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&GT; user.save      (0.1ms)SAVEPOINT active_record_1      (0.1ms)ROLLBACK TO SAVEPOINT active_record_1   ArgumentError:参数数量错误(1表示0)           来自/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: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新&#39;           来自/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:在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&#39;           来自/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&#39;           来自/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插入&#39;           来自/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&#39;           来自/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&#39;中的_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块中           来自/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&#39;           来自/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&#39;   ...... 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 {{ 1}}控制台&#39;           来自/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:在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&#39;           来自/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:在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块中的require&#39;           来自/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: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要求&#39;           from / Users / Jonathon / Documents / Aptana Studio 3 Workspace / RoR / bin / rails:8:in 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 load&#39;           来自/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:在<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 load_dependency&#39;           来自/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: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要求&#39;           来自/Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in load' from /Users/Jonathon/.rbenv/versions/2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in&#39; irb(main):003: 0 GT;

我已将我的user.rb文件放在此处:

require' from -e:1:in

和我的create_users.rb文件也在这里

class User < ActiveRecord::Base end

感谢并告诉我是否还有我需要提供的信息!

2 个答案:

答案 0 :(得分:0)

感谢所有帮助人员!

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

答案 1 :(得分:0)

我遇到了同样的问题。当我像你一样尝试ArgumentError时,我首先得到user.save。然后当我尝试rake db:drop然后rake db:migrate时,我也遇到了同样的错误。

我的案例是在bundle update之后,我发现这个arel gem版本出现了问题。它在Gemfile.lock上显示arel (6.0.0),但是当我像之前一样尝试arel (6.0.0.beta2)时,它就有用了。

我不知道为什么。我找到了this discussion。现在我们对非beta Rails 4.2.0没有问题,如果我们不在示例应用程序上bundle update(正如作者所说),那么Hartl教程没有问题