我在网上搜索了类似的问题,但似乎没有一个符合我的具体问题。如果我在控制台中创建用户并保存,我告诉他们有效。但是,当我在本地环境中创建用户时,并询问控制台是否有效,我告诉他们他们是假的,并收到以下错误消息:
NameError: undefined local variable or method `user' for main:Object
from (irb):4
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/console.rb:110:in `start'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/console.rb:9:in `start'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require'
from /Users/RichardEssig/Desktop/coding/freedom/bin/rails:9:in `<top (required)>'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/RichardEssig/.rvm/gems/ruby-2.2.1/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/RichardEssig/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/RichardEssig/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
这是我的用户模型代码:
before_save { self.email = email.downcase }
validates :name, :industry, :company, :title, :street, :state, :city, :zip, presence: true
validates :phone, presence: true, length: { maximum: 11 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
validate :password_complexity
validates :password, presence: true, length: { minimum: 6 }
def password_complexity
return unless password.present?
unless password =~ /[a-z]/ && password =~ /[A-Z]/ && password =~ /[\d]/
errors.add :password, "must include at least one lowercase letter, one uppercase letter, and one digit"
end
end
end
为什么我的用户无效?它们出现在本地环境中,我可以更新并销毁它们。这里很困惑。
我在views / users / new表单中保存新用户,如下所示:
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> </h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
.
.
.
<div class="actions">
<%= f.submit %>
</div>
<% end %>
关于错误消息,在我通知用户是假的之后,我在控制台中键入以下代码:
user.errors.full_messages