因此,我遵循了2011年关于如何创建简单的rails应用程序的指南。在这种情况下,URL缩短。
http://www.sitepoint.com/building-your-first-rails-application-models/
我在测试模型的部分。我需要打开Rails控制台并输入puts Rails.env
来检查开发环境。
迁移:
class CreateUrls < ActiveRecord::Migration
def self.up
create_table :urls do |t|
t.string :url, :null => false
t.timestamps
end
end
def self.down
drop_table :urls
end
end
网址模型:
class Url < ActiveRecord::Base
validates :url, :presence > true
end
当我输入命令u = Url.new
时,我遇到了一堵错误。我怀疑这些可能与Ruby和Rails的版本有关吗?
Loading development environment (Rails 4.1.1)
irb(main):001:0> u = Url.new
ArgumentError: comparison of Symbol with true failed
from /Users/tomcox/development/projects/toolong/app/models/url.rb:2:in `>'
from /Users/tomcox/development/projects/toolong/app/models/url.rb:2:in `<class:Url>'
from /Users/tomcox/development/projects/toolong/app/models/url.rb:1:in `<top (required)>'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:443:in `load'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:443:in `block in load_file'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:633:in `new_constants_in'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:442:in `load_file'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:342:in `require_or_load'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:480:in `load_missing_constant'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:180:in `const_missing'
from (irb):1
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `block in require'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
from /Users/tomcox/development/projects/toolong/bin/rails:8:in `<top (required)>'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/tomcox/.rbenv/versions/2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
答案 0 :(得分:4)
你有一个错字:
validates :url, :presence > true
应该是:
validates :url, :presence => true