質問

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?

役に立ちましたか?

解決

You can try the code below:

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

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top