Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get environment settings in rails controller

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.

like image 626
Anil D Avatar asked Apr 11 '12 07:04

Anil D


People also ask

How do I view environment variables in Ruby?

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.

What is environment variables in Rails?

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.

What does Before_action do in Rails?

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.

What variable changes the environment in Ruby?

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.


2 Answers

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
like image 169
Ernest Avatar answered Oct 22 '22 07:10

Ernest


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)

like image 38
Vik Avatar answered Oct 22 '22 08:10

Vik