You can try the code below:
class ...
path = File.expand_path('../../app/views/layouts/application.html.erb', __FILE__)
content = File.read(path)
end
Question
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?
Solution
You can try the code below:
class ...
path = File.expand_path('../../app/views/layouts/application.html.erb', __FILE__)
content = File.read(path)
end
OTHER TIPS
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")