【发布时间】:2016-02-12 21:25:20
【问题描述】:
我正在尝试在 seed.rb 文件中创建一些数据。当我尝试执行rake db:seed 时出现此错误:
$ rake db:seed
/Users/romenigld/workspace/ebook/beginning_rails_4_3rd_edition/blog/db/seeds.rb:1: warning: encountered \r in middle of line, treated as a mere space
rake aborted!
SyntaxError: /Users/romenigld/workspace/ebook/beginning_rails_4_3rd_edition/blog/db/seeds.rb:1: syntax error, unexpected tCONSTANT, expecting end-of-input
Category.create([{:name => 'Programmi...
... ^
/Users/romenigld/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/Users/romenigld/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/romenigld/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/romenigld/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/Users/romenigld/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/engine.rb:547:in `load_seed'
/Users/romenigld/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Users/romenigld/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
我的seed.rb 文件:
user = User.create :email => 'mary@example.com', :password => 'guessit'
Category.create([{:name => 'Programming'},
{:name => 'Event'},
{:name => 'Travel'},
{:name => 'Music'},
{:name => 'TV'}])
我的用户.rb:
class User < ActiveRecord::Base
has_one :profile
has_many :articles, -> { order('published_at DESC, title ASC')},
:dependent => :nullify
end
我的类别.rb:
class Category < ActiveRecord::Base
has_and_belongs_to_many :articles
end
我的文章.rb:
class Article < ActiveRecord::Base
validates_presence_of :title, :body
belongs_to :user
has_and_belongs_to_many :categories
def long_title
"#{title} - #{published_at}"
end
end
在我创建articles_categories 之前:
class CreateArticlesCategories < ActiveRecord::Migration
def change
create_table :articles_categories, :id => false do |t|
t.references :article
t.references :category
end
end
def self.down
drop_table :articles_categories
end
end
并创建模型类别:
class CreateArticlesCategories < ActiveRecord::Migration
def change
create_table :articles_categories, :id => false do |t|
t.references :article
t.references :category
end
end
def self.down
drop_table :articles_categories
end
end
【问题讨论】:
-
这是你的整个
seeds.rb文件,还是只是一个sn-p? -
你好 michael 我正在关注一本电子书,现在这就是全部内容。谢谢!
-
如果您复制并粘贴了此代码,请尝试以下 Marko 的答案。从错误上方的警告来看,该行包含一个 \r 字符,这可能会导致您的错误。它是一个回车符,所以你只看到一个正常的换行符。
标签: ruby-on-rails ruby ruby-on-rails-4 rake