I'm trying to use the evernote gem to access the Evernote API. In those instructions, it says to create a config file containing the API account details, and then load the config file as follows:
我正在尝试使用evernote gem访问Evernote API。在这些说明中,它说要创建包含API帐户详细信息的配置文件,然后按如下方式加载配置文件:
config = File.dirname(__FILE__) + "/config.yml"
user_store = Evernote::UserStore.new(user_store_url, config, "sandbox")
I created a file evernote.yml
in the config folder, and put the following code in the home
action in pages_controller.rb
我在config文件夹中创建了一个文件evernote.yml,并将以下代码放在pages_controller.rb中的home操作中
config = File.dirname(__FILE__) + "/evernote.yml"
user_store = Evernote::UserStore.new(user_store_url, config, "sandbox")
When the code is run, I get this error on the 2nd line
代码运行时,我在第二行收到此错误
Errno::ENOENT in PagesController#home
No such file or directory - /Users/ben/rails_projects/evernote_app/app/controllers/evernote.yml
How do I load the config file without getting this error?
如何在不收到此错误的情况下加载配置文件?
3 个解决方案
#1
16
The problem is that File.dirname(__FILE__)
points to the directory of the current file, which happens to be a controller. You want to point to config directory under your rails root. To make this work I would do the following:
问题是File.dirname(__ FILE__)指向当前文件的目录,该目录恰好是一个控制器。您想指向rails根目录下的config目录。为了完成这项工作,我会做以下事情:
config = File.join(Rails.root, 'config', 'evernote.yml')
user_store = Evernote::UserStore.new(user_store_url, config, "sandbox")
#2
7
Try this,
尝试这个,
config_file_path = "#{Rails.root}/config/config.yml" # or this to load yaml directly config = YAML::load(File.open("#{Rails.root}/config/config.yml"))
Rails.root
gives you the path of Rails application root folder
Rails.root为您提供Rails应用程序根文件夹的路径
#3
1
I have the need for a config is just about ever Rails app I've ever worked with. Here's what I use, put this in config/application.rb in before the module definition of your application:
我需要一个配置就是我曾经使用过的Rails应用程序。这是我使用的,在应用程序的模块定义之前将它放在config / application.rb中:
# Load config/config.yml into APP_CONFIG
APP_CONFIG = YAML.load(ERB.new(IO.read(File.expand_path('../config.yml', __FILE__))).result)[Rails.env]
The main difference with this version is it runs the config file through ERB, which can be useful if you want to use ruby variables and stuff in the config.
与此版本的主要区别在于它通过ERB运行配置文件,如果您想在配置中使用ruby变量和内容,这可能很有用。
#1
16
The problem is that File.dirname(__FILE__)
points to the directory of the current file, which happens to be a controller. You want to point to config directory under your rails root. To make this work I would do the following:
问题是File.dirname(__ FILE__)指向当前文件的目录,该目录恰好是一个控制器。您想指向rails根目录下的config目录。为了完成这项工作,我会做以下事情:
config = File.join(Rails.root, 'config', 'evernote.yml')
user_store = Evernote::UserStore.new(user_store_url, config, "sandbox")
#2
7
Try this,
尝试这个,
config_file_path = "#{Rails.root}/config/config.yml" # or this to load yaml directly config = YAML::load(File.open("#{Rails.root}/config/config.yml"))
Rails.root
gives you the path of Rails application root folder
Rails.root为您提供Rails应用程序根文件夹的路径
#3
1
I have the need for a config is just about ever Rails app I've ever worked with. Here's what I use, put this in config/application.rb in before the module definition of your application:
我需要一个配置就是我曾经使用过的Rails应用程序。这是我使用的,在应用程序的模块定义之前将它放在config / application.rb中:
# Load config/config.yml into APP_CONFIG
APP_CONFIG = YAML.load(ERB.new(IO.read(File.expand_path('../config.yml', __FILE__))).result)[Rails.env]
The main difference with this version is it runs the config file through ERB, which can be useful if you want to use ruby variables and stuff in the config.
与此版本的主要区别在于它通过ERB运行配置文件,如果您想在配置中使用ruby变量和内容,这可能很有用。