I have a problem I am following step by step this tutorial to include the gem stripe to my rails application : https://stripe.com/docs/checkout/rails
But I have an issue when I launch my payment:
you did not set a valid publishable key. Call Stripe.setPublishableKey() with your publishable key.
The tutorial I am following doesn't use this method Stripe.setPublishableKey() and it seems working as well ...
I am using a .env file to get the API keys and I use the test keys from the tutorial.
Any idea for the solution ?!
Thank you very much :) If you need more information please tell me
My actual code is a copy/paste from the tutorial I use.
Edit 1
I don't understand why but it working when I push the project on Heroku ... but I still don't understand why it's failing on local
From the guide you linked:
The application makes use of your publishable and secret API keys
to interact with Stripe. An initializer is a good place to set these
values, which will be provided when the application is started.
Add the following to config/initializers/stripe.rb:
Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'],
:secret_key => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
These keys values are pulled out of environmental variables so as not
to hardcode them. It’s best practice not to write API keys into your
code, where they could easily wind up in source control repositories
and other non-private destinations.
Have you added that data into config/initializers/stripe.rb ?
If so, you may need to restart your server as the files in config/initializers are only loaded on server startup
I am using a .env file to get the API keys and I use the test keys from the tutorial.
Rails don't autoload environment variables from .env file. So, in the tutorial author set required variables from the console before server start, but you use .env file.
To fix that use gem dotenv-rails: just add in your Gemfile(not need add the same lines as in gem description)
I was able to overcome the same type of error in another tutorial by changing the last line of the stripe.rb file to:
Stripe.api_key = Rails.application.credentials.stripe[:secret_key]
with the key being saved in the credentials.yml.enc file in rails 5.2
There is the following task: My app has got one variable with name 'delivery_time'; this variable can be changed through admin panel. I think it's not a good way to store it in some database table. How can I store it in configs or anything else? May be I should store it in db yet?
You can easily store settings in the config file like this:
Create config.yml and store your settings
delivery_time: '...'
Add this config to initializers/load_config.rb
require 'ostruct'
require 'yaml'
app_config = YAML.load_file(File.join(::Rails.root, 'config', 'config.yml'))[Rails.env]
::AppConfig = OpenStruct.new app_config
And you can call it AppConfig.delivery_time anywhere in the project
Basically for static settings we use following gem
https://github.com/railsjedi/rails_config
so this gem will generate following file
config/settings.yml
you can define your variable(my_config) in this yml file. and use it throughout your application as follows:
Settings.my_config
Or you can use any similar gems
you can use PStore that implements a file based persistence mechanism based on a Hash.
OR
yaml based approach
I'm using the bigQuery gem in my project. I initialize it with options client_id, service_email, key, project_id, and dataset. I have my .p12 file from Google that I want to pass in but I'm not sure where to put the file in my Rails project - I tried putting it in the same directory as my config file and passing the relative path name, but I'm getting an invalid passphrase error. Specifically, line 10 is throwing an error when load_key is being called:
key = Google::APIClient::PKCS12.load_key(
opts['key'],
"notasecret"
)
So clearly it's not loading the key file correctly. I'm terrible at Rails asset control - where should I put my keyfile and what pathname should I pass in my options hash?
You could place the keyfile into the config directory, then do the following:
opts['key'] = Rails.root.join('config','nameofkeyfile.p12').to_s
You don't want the key to be in a location that your application will serve up to the public, so config sounds like a good location to me.
You can experiment with the block you have above in the Rails console:
# run `rails c` then
keypath = Rails.root.join('config','nameofkeyfile.p12').to_s
key = Google::APIClient::PKCS12.load_key(keypath, "notasecret")
Looking at the Google::APIClient documentation, I see load_key is deprecated. They recommend using Google::APIClient::KeyUtils instead.
key = Google::APIClient::KeyUtils.load_from_pkcs12(keyfile, "notasecret")
As for a quick overview of the Rails asset pipeline, see here. (Please pardon the "for dummies" part of that url, it appears to be good, quick info.)
I am using Wabler gem to generate the war file for our Ruby on Rails application. By default, it generates a WAR file named the Rails project home directory name by default.
I would like to know if it is possible to specify custom war file name instead of the default project home directory name.
Thanks
Noman A.
Found answer to my question, so sharing with all who might need it.
File to modify:
warble.rb
For our application, this resides inside [project_dir]/config/
This file would have a section like:
Warbler::Config.new do |config|
.....
.....
end
Add the following line to specify custom war/jar file name (without extension):
config.jar_name = "testname"
So, the segment would look like:
Warbler::Config.new do |config|
.....
.....
config.jar_name = "testname"
end
Note: The "...." shown above are not part of the actual configuration, I have replaced original values for example purpose.
Now, when I run warble war/ rake war, it generates war file as "testname.war"
Hope this might help another newbie like me!
Thanks
Noman A.
Add config/warble.rb file.
Warbler::Config.new do |config|
config.war_name = "test_war_name"
end
Edit: config.war_name is deprecated, see logged here
# Deprecated
def war_name
$stderr.puts "config.war_name deprecated; replace with config.jar_name" #:nocov:
jar_name #:nocov:
end
Use the following:
Warbler::Config.new do |config|
config.jar_name = "test_war_name"
end
I was wondering how to add custom configuration variables to a Rails application and how to access them in the controller?
Secondly, I was planning to have S3 support for uploads in my application, if I wanted to add a yaml file with the S3 access, secret key, how do I initialize it in my Rails App and how do I access the values that I have defined in that config file.
In Rails 3, Application specific custom configuration data can be placed in the application configuration object. The configuration can be assigned in the initialization files or the environment files -- say for a given application MyApp:
MyApp::Application.config.custom_config_variable = :my_config_setting
or
Rails.configuration.custom_config_variable = :my_config_setting
To read the setting, simply call the configuration variable without setting it:
Rails.configuration.custom_config_variable
=> :my_config_setting
UPDATE Rails 4
In Rails 4 there a new way for this => http://guides.rubyonrails.org/configuring.html#custom-configuration
Update 1
Very recommended: I'm going with Rails Config gem nowadays for the fine grained control it provides.
Update2
If you want a quick solution, then check Jack Pratt's answer below.
Although my original answer below still works, this answer is now outdated. I recommend looking at updates 1 and 2.
Original Answer:
For a quick solution, watching the "YAML Configuration File" screen cast by Ryan Bates should be very helpful.
In summary:
# config/initializers/load_config.rb
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
# application.rb
if APP_CONFIG['perform_authentication']
# Do stuff
end
In Rails 3.0.5, the following approach worked for me:
In config/environments/development.rb, write
config.custom_config_key = :config_value
The value custom_config_key can then be referenced from other files using
Rails.application.config.custom_config_key
In Rails 4
Assuming you put your custom variables into a yaml file:
# config/acme.yml
development:
:api_user: 'joe'
:api_pass: 's4cret'
:timeout: 20
Create an initializer to load them:
# config/initializers/acme.rb
acme_config = Rails.application.config_for :acme
Rails.application.configure do
config.acme = ActiveSupport::OrderedOptions.new
config.acme.api_user = acme_config[:api_user]
config.acme.api_pass = acme_config[:api_pass]
config.acme.timeout = acme_config[:timeout]
end
Now anywhere in your app you can access these values like so:
Rails.configuration.acme.api_user
It is convenient that Rails.application.config_for :acme will load your acme.yml and use the correct environment.
This works in rails 3.1:
in config/environment.rb (or in config/environments/.. to target a specific environment) :
YourApp::Application.config.yourKey = 'foo'
This will be accessible in controller or views like this:
YourApp::Application.config.yourKey
(YourApp should be replaced by your application name.)
Note: It's Ruby code, so if you have a lot of config keys, you can do this :
in config/environment.rb :
YourApp::Application.configure do
config.something = foo
config.....
config....
.
config....
end
Since Rails 4.2, without additional gems, you can load config/hi.yml simply by using Rails.application.config_for :hi.
For example:
touch config/passwords.yml
#config/passwords.yml
development:
username: 'a'
password: 'b'
production:
username: 'aa'
password: 'bb'
touch config/initializers/constants.rb
#config/initializers/constants.rb
AUTHENTICATION = Rails.application.config_for :passwords
and now you can use AUTHENTICATION constant everywhere in your application:
#rails c production
:001> AUTHENTICATION['username'] => 'aa'
then add passwords.yml to .gitignore: echo /config/passwords.yml >> .gitignore, create an example file for your comfort cp /config/passwords.yml /config/passwords.example.yml and then just edit your example file in your production console with actual production values.
Rails 6 and 7
Many outdated answers, so adding one that is specific to Rails 6.
Application specific configuration goes in initializer files. Details are here: edge guides
Example:
config/initializers/foo.rb
module MyApp
class Application < Rails::Application
config.test_val = 'foo'
end
end
Alternatively:
Rails.application.config.test_val = 'foo'
This can now be accessed as:
Rails.configuration.test_val
Many more possibilities.
edge guides #custom-configuration
ex, you can also set up nested namespace configurations:
config.x.payment_processing.schedule = :daily
config.x.payment_processing.retries = 3
config.super_debugger = true
or use config_for to load entire custom config files:
config/payment.yml
production:
environment: production
merchant_id: production_merchant_id
public_key: production_public_key
private_key: production_private_key
development:
environment: sandbox
merchant_id: development_merchant_id
public_key: development_public_key
private_key: development_private_key
Then load it with:
config/initializers/load_payment.rb
module MyApp
class Application < Rails::Application
config.payment = config_for(:payment)
end
end
I just wanted to update this for the latest cool stuff in Rails 4.2, you can now do this inside any of your config/**/*.rb files:
config.x.whatever.you.want = 42
...and this will be available in your app as:
Rails.configuration.x.whatever.you.want
See more here: http://guides.rubyonrails.org/configuring.html#custom-configuration
Check out this neat gem doing exactly that:
https://github.com/mislav/choices
This way your sensitive data won't be exposed in open source projects
I created a simple plugin for YAML settings: Yettings
It works in a similar fashion to the code in khelll's answer, but you only need to add this YAML configuration file:
app/config/yetting.yml
The plugin dynamically creates a class that allows you to access the YML settings as class methods in your app like so:
Yetting.your_setting
Also, if you want to use multiple settings files with unique names, you can place them in a subdirectory inside app/config like this:
app/config/yettings/first.yml
app/config/yettings/second.yml
Then you can access the values like this:
FirstYetting.your_setting
SecondYetting.your_setting
It also provides you with default settings that can be overridden per environment. You can also use erb inside the yml file.
I really like the settingslogic gem. Very easy to set up and use.
https://github.com/binarylogic/settingslogic
If you use Heroku or otherwise have need to keep your application settings as environment variables, the figaro gem is very helpful.
I like to use rails-settings for global configuration values that need to be changeable via web interface.
Something we've starting doing at work is the ActiveSupport Ordered Hash
Which allows you to define your configuration cleanly inside the environment files e.g.
config.service = ActiveSupport::OrderedOptions.new
config.service.api_key = ENV['SERVICE_API_KEY']
config.service.shared_secret = ENV['SERVICE_SHARED_SECRET']
I would suggest good approach how to deal with configuration in your application at all. There are three basic rules:
change your configuration not a code;
use configurations over conditions;
write code that means something.
To have more detailed overview follow this link: Rails configuration in the proper way