创建新用户 - 用户存在?

时间:2015-08-10 19:26:10

标签: ruby-on-rails

我去创建user=User.new (:screen_name => "jeff holmes", :email "jeff92@jeff.com", :password "1234")我有一些验证,我试图看看是否会通过:

class User < ActiveRecord::Base

  validates_uniqueness_of :screen_name, :email
  validates_length_of :screen_name, :within => 4...20
  validates_length_of :password, :within => 4...20
  validates_length_of :email, :within => 6...50

  def validate
    errors.add(:email, 'must be valid.') unless email.include? ("@")
    errors.add(:screen_name, 'cannot include spaces.') if screen_name.include?(" ")
  end
end

在我尝试创建用户后,弹出所有这些东西,我不知道它是什么。

NameError: undefined local variable or method `user' for main:Object
        from (irb):3
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/console.rb:110:in `start'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
        from /Users/coreyholmes/RubymineProjects/worklink/bin/rails:8:in `<top (required)>'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'

然后弹出所有内容后,我会user.save,然后会出现。

NameError: undefined local variable or method `user' for main:Object

怎么回事?我在这里做错了什么。

由于

2 个答案:

答案 0 :(得分:0)

目前的问题不是100%肯定,但这里有一个关于创建过程的概述

user = User.new # creates the user object/instance but DOES NOT save or populate the ID column/attribute/method
# OR ... 
user = User.new(params[:user]) # often what you see in a controller action
user.valid? # checks to ensure your validations pass ... duh :) 
user.save # saves the users
# from here, you can do a few things: 
user.persisted? # this checks if the user was saved in the DB
user.id 
# will now show you the user's id ... 
# in your original post's code it appears you check for the id _before_ the save and it has to be saved before that attribute/method is populated. 

User Exists是Rails在尝试保存之前检查该用户是否已经在数据库中(我的意思是您的用户模型验证了emailscreen_name方法/ attributes)

至于在用户创建后看到用户,你可以做几件事:

  1. 如果您在控制台中,只需输入user即可获得输出。
  2. 在网页视图(阅读:浏览器)中,您将转到http://localhost:3000/users/ID_OF_NEW_USER(这假设您在UsersController和show视图中有一个show动作(show.html.erb或show.html.haml) ......如果你生成了一个脚手架(rails generate scaffold User email:string ...,你就有了这些!)
  3. 同样,我并不完全确定我理解你的要求,但我希望这有助于你更好地理解这一流程。

    更新: 刚看到你帖子的补充内容。此代码不正确:

    user=User.new (:screen_name => "jeff holmes", :email "jeff92@jeff.com", :password "1234")
    

    应该是

    user=User.new (:screen_name => "jeff_holmes", :email => "jeff92@jeff.com", :password => "1234") # note the missing '=>' for email and password and the space in the screen_name, which is invalid according to the model
    

答案 1 :(得分:0)

你做得很好。

user = User.create name: "name", email: "email@test.com", password: "123456"

通过调用类的create方法来运行所有验证。除非他们调用save方法失败。既然用户是new_object? rails将创建用户。

之后,您在变量&#34; user&#34;中拥有用户实例。

另一种方式是

user = User.new
user.email = "test@test.de"
....
user.save

再次,save方法将调用验证,如果它们通过它将把对象保存到数据库。

您可以使用user.id或User.last获取ID(这将从数据库中加载最新用户)。

测试这些行为并使用它的最佳方法是rails控制台。您可以使用 rails c

运行它 你做得很好。

只是一点点提示:看看名为&#34;设计的宝石&#34;它可在15分钟内为您提供完整的用户身份验证逻辑。不需要编写代码行。