Rails-当验证器取消注释时,某些东西停止工作

时间:2011-05-03 16:31:16

标签: ruby-on-rails-3

当我取消注释'validates_presence_of:username'时,当我打开下面的视图时,会发生错误'无法将字符串转换为整数'。

重启服务器无济于事。

型号:

require 'digest/sha1'
class User < ActiveRecord::Base
#validates_uniqueness_of :username
  validates_presence_of :username
  #has_many :votes
  #has_many :sentences, :through => :votes

  def self.make_salt(username="")
    Digest::SHA1.hexdigest(username+"#{Time.now}#{Kernel.rand}")
  end

  def self.hash(password="")
    Digest::SHA1.hexdigest(password)
  end
end

查看:

<h2>Users</h2>
<table class="listing" summary="Users list">
  <tr class="header">
    <th>Username</th>
    <th>Actions</th>
  </tr>
  <% @users.each do |user| %>
  <tr>
    <td><%= user.username %></td>
    <td>
      <%= link_to('Show',{:action => 'show',:id => user.id}) %>
    </td>
  </tr>
  <% end %>
</table>

应用程序跟踪为空白。

框架追踪:

activesupport (3.0.7) lib/active_support/descendants_tracker.rb:23:in `delete'
activesupport (3.0.7) lib/active_support/descendants_tracker.rb:23:in `clear'
activesupport (3.0.7) lib/active_support/descendants_tracker.rb:21:in `each'
activesupport (3.0.7) lib/active_support/descendants_tracker.rb:21:in `clear'
railties (3.0.7) lib/rails/application/bootstrap.rb:59:in `_callback_after_1'
activesupport (3.0.7) lib/active_support/callbacks.rb:421:in `_run_call_callbacks'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.2) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.7) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.2) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.7) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.2) lib/rack/lock.rb:11:in `call'
rack (1.2.2) lib/rack/lock.rb:11:in `synchronize'
rack (1.2.2) lib/rack/lock.rb:11:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.7) lib/rails/application.rb:168:in `call'
railties (3.0.7) lib/rails/application.rb:77:in `send'
railties (3.0.7) lib/rails/application.rb:77:in `method_missing'
railties (3.0.7) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.2) lib/rack/content_length.rb:13:in `call'
rack (1.2.2) lib/rack/handler/webrick.rb:52:in `service'
/usr/lib64/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib64/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib64/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib64/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib64/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib64/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib64/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib64/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib64/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib64/ruby/1.8/webrick/server.rb:82:in `start'
rack (1.2.2) lib/rack/handler/webrick.rb:13:in `run'
rack (1.2.2) lib/rack/server.rb:213:in `start'
railties (3.0.7) lib/rails/commands/server.rb:65:in `start'
railties (3.0.7) lib/rails/commands.rb:30
railties (3.0.7) lib/rails/commands.rb:27:in `tap'
railties (3.0.7) lib/rails/commands.rb:27
script/rails:6:in `require'
script/rails:6

Scheme.rb :(部分与用户表)

create_table "users", :force => true do |t|
  t.string   "username"
  t.string   "e-mail"
  t.string   "hashed_password"
  t.string   "salt"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "users", ["username"], :name => "index_users_on_username"

为什么会这样?

1 个答案:

答案 0 :(得分:0)

在迁移中尝试此操作: add_index“users”,[“username”],:name =&gt; “index_users_on_username”,unique =&gt;真

也许他可以解决它,如果你真的想在DB中验证用户名的唯一性,那就更好了