Ruby-on-rails 在 Rails 4 中,我可以在哪里存储站点范围的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20857886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:10:33  来源:igfitidea点击:

Where can I store site-wide variables in Rails 4?

ruby-on-railsrubyruby-on-rails-4

提问by Michael Giovanni Pumo

I am new to Rails and come from a ColdFusion background, where we would store global / site-wide variables in the 'application' scope. This persists the variable across any view or controller. Does Rails 4 have an equivalent functionality for this type of thing?

我是 Rails 的新手并且来自 ColdFusion 背景,我们将在“应用程序”范围内存储全局/站点范围的变量。这会在任何视图或控制器中保持变量。Rails 4 是否具有此类功能的等效功能?

The site-wide variable won't typically change often so it doesn't need protecting in any way.

站点范围的变量通常不会经常更改,因此不需要以任何方式进行保护。

For example, in my situation, I want to store the website's domain name. One for testing and one for live environments. Localhost for development and xxxxxx.com for production.

例如,在我的情况下,我想存储网站的域名。一种用于测试,一种用于实时环境。用于开发的本地主机和用于生产的 xxxxxx.com。

Any tips or pointers would help. I have Googled this extensively and solutions seem to be far too complicated to achieve what seems to be such a trivial task. What's the best elegant solution for Rails 4?

任何提示或指示都会有所帮助。我已经在谷歌上广泛地搜索了这个,解决方案似乎太复杂了,无法完成似乎如此微不足道的任务。Rails 4 的最佳优雅解决方案是什么?

回答by Simone Carletti

The simplest, basic and default way is to use the Rails.application.configstore.

最简单、基本和默认的方式是使用Rails.application.config商店。

Rails.application.config.my_config = 'foo'

You can assign a config in your environment:

您可以在您的环境中分配一个配置:

# application.rb
module MyApp
  class Application < Rails::Application
    config.my_config = 'foo'
  end
end

and read it with

并阅读它

Rails.application.config.my_config
# => 'foo'

This approach works well for very simple applications, but if you want something more advanced there are several gems available.

这种方法适用于非常简单的应用程序,但如果你想要更高级的东西,有几个 gems 可用。

I'm currently using SimpleConfig. The main advantages are:

我目前正在使用SimpleConfig. 主要优点是:

  • per-environment configuration. You can configure default configurations for the application, then override defaults with environment specific configurations
  • local.rbfile for custom overrides
  • capistrano-like configuration style
  • it works nicely with the dotenvgem, very useful to avoid storing sensitive credentials in your repo.
  • 每个环境的配置。您可以为应用程序配置默认配置,然后使用特定于环境的配置覆盖默认值
  • local.rb自定义覆盖文件
  • capistrano- 类似配置风格
  • 它与dotenvgem很好地配合使用,对于避免在您的存储库中存储敏感凭据非常有用。

回答by ckruse

This sounds like a perfect example for configuration values stored in config/environments/production.rband config/environments/development.rb. Just store any value there:

这听起来像是存储在config/environments/production.rb和 中的配置值的完美示例config/environments/development.rb。只需在那里存储任何值:

config.my_special_value = 'val'

And access it in your application like this:

并在您的应用程序中访问它,如下所示:

Rails.application.config.my_special_value

Always the value of your environment is active.

您的环境的价值始终是活跃的。

If you just want to have a ?global“ value, store it in your application controller. All your view controllers are derived from your app controller, so you can save any value there as an instance or class variable:

如果您只想拥有一个“全局”值,请将其存储在您的应用程序控制器中。你所有的视图控制器都是从你的应用控制器派生出来的,所以你可以将任何值保存为实例或类变量:

class ApplicationController < ActionController::Base
  MY_CONSTANT_VALUE = "foo"
end

class MyViewController < ApplicationController
  def index
    raise MY_CONSTANT_VALUE.inspect
  end
end

You also could implement an helper:

您还可以实现一个助手:

# app/helpers/application_helper.rb
module ApplicationHelper
  FOO = "bar"
end

# app/controllers/foo_controller.rb
class FooController < ApplicationController
  def index
    raise FOO
  end
end

回答by Зелёный

I can recommend good method to store variable. I use this on production
Passwords can be stored easier to .envfile http://i.stack.imgur.com/jbcAO.png
like this

我可以推荐存储变量的好方法。我用这对生产
的密码可以存储更容易.env文件 http://i.stack.imgur.com/jbcAO.png
这样

#Root dir create file ".env"
PASSWORD=123456

and load password

并加载密码

#Somewhere in app
ENV['PASSWORD'] #=> 123456

it works I hope will help you

它有效,我希望能帮助你

enter image description here

在此处输入图片说明

回答by Raj Adroit

You can use gem figaro

你可以使用宝石费加罗

write your variables in config/application.yml

config/application.yml 中写入变量

HELLO: world
development:
  HELLO: developers
production:
  HELLO: users

Then you can fetch

然后你可以取

ENV["HELLO"]

回答by Mohammad Shahnawaz

In rails there is gem named as

在 rails 中有一个 gem 命名为

gem 'dotenv-rails'

By using it we can assign the variables to system level and used in application.

通过使用它,我们可以将变量分配到系统级别并在应用程序中使用。

By using simple steps First create a simple filed in system level at any place with named extension .env

通过使用简单的步骤首先在系统级别的任何地方创建一个名为 .env 的简单文件

//in application.rb        
require 'dotenv'
Dotenv.load('path-of-your-file.env')

And restart your application

并重新启动您的应用程序

Source Please got the link for the desscription of dot env gem

来源 请获取有关 dot env gem 描述的链接