Frage

I need to insert the contents of a file into the database during a migration (Rails 3.2.13). What's the proper way to reference a file that is elsewhere in the project?

db/migrate/the_migration.rb

class ...
  content = File.read("../../app/views/layours/application.html.erb")
end

The relative path doesn't seem to work - I get:

No such file or directory - ../../app/views/layouts/application.html.erb

How can I map this path to an absolute path?

War es hilfreich?

Lösung

You can try the code below:

class ...
  path = File.expand_path('../../app/views/layouts/application.html.erb', __FILE__)
  content = File.read(path)
end

Andere Tipps

assuming you are using rake to apply an active record migration. The file path will be relative to where you started rake which I'm sure will be the projects root.

The file path would be:

content = File.read("app/views/layouts/application.html.erb")

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top