I have some email setting in development.rb which i want to access in my controller.
Settings in development.rb
are:
config.notify_submited_transaction = '[email protected],[email protected]'
config.notify_approved_transaction = '[email protected]'
In my controller/action I am trying this:
@to = Rails.env.notify_submited_transaction
@subject = 'AM - Vendor Submitted Transaction'
AmMailer.vendor_submited_transaction(@to, @subject, current_user).deliver
This though results in error:
undefined method `notify_submited_transaction'
I am not sure how to get config value I've set.
Thanks for any help.
Accessing Environment Variables from Ruby Ruby has direct access to environment variables via the ENV hash. Environment variables can be directly read or written to by using the index operator with a string argument.
Environment variables contain a name and value, and provide a simple way to share configuration settings between multiple applications and processes in Linux. For example, Cloud 66 creates environment variables for your database server address, which can be referenced in your code.
When writing controllers in Ruby on rails, using before_action (used to be called before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It's what you want to use to "prepare" the data necessary before the action executes.
Now, we have to Edit the “Path” variable under System variables so that it also contains the path to the Ruby environment. Under the System Variable select the “Path” variable and click on Edit button.
Just a sidenote: Rails.env is special string object, that allows you to get current environment (its not like Rack's env):
puts Rails.env # => "production"
puts Rails.env.test? # => false
It's not meant to return config settings.
This may come in handy when you want to put your custom settings under /config/initializers/*
, and for clarity, it's a better way in some cases (it's recommended not to clutter rails environment files with your custom settings). For example:
# config/initializers/mailer_settings.rb
if Rails.env.production?
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
...
}
else
#different settings
end
Try to access :
Rails.application.config.notify_submited_transaction
Rails.application.config.notify_approved_transaction
Seems similar to : For Rails, how to access or print out config variables (as experiment or test / debugging)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With