Getting error in seed.rb when I do rake db:seed was aborted

时间:2016-02-12 21:25:21

标签: ruby-on-rails ruby ruby-on-rails-4 rake

I'm trying to create some data in the seed.rb file. When I try to do the "Tags": [ "#c", "#c", "#coucou", "#cd" ], I have this error:

rake db:seed

My seed.rb file:

    $ 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)

My user.rb:

user = User.create :email => 'mary@example.com', :password => 'guessit'

Category.create([{:name => 'Programming'},
                 {:name => 'Event'},
                 {:name => 'Travel'},
                 {:name => 'Music'},
                 {:name => 'TV'}])

My category.rb:

class User < ActiveRecord::Base
  has_one :profile
  has_many :articles, -> { order('published_at DESC, title ASC')},
                      :dependent => :nullify
end

My article.rb:

class Category < ActiveRecord::Base
  has_and_belongs_to_many :articles
end

Before I create an articles_categories:

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

and create the model Category:

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

2 个答案:

答案 0 :(得分:0)

The error you're receiving is simply a syntax error. This is some code from the example Rails provides in their seed.rb

$tor = array_map('trim', $tor);

Here is your code.

# Examples:
#
#   cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
#   Mayor.create(name: 'Emanuel', city: cities.first)

Try reformatting your code to the syntax the example provides -

Category.create([{:name => 'Programming'},
             {:name => 'Event'},
             {:name => 'Travel'},
             {:name => 'Music'},
             {:name => 'TV'}])

I believe with seeding the db the only time you use hash rockets is outside of an array in reference to seeding, I could be wrong though, just a thought.

Hope this helps!

答案 1 :(得分:0)

你有没有复制&amp;从某个地方粘贴代码?如果是这样,请尝试删除该文件并自行重新键入代码。