8

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 = 'anil@swiftsetup.com,anildbest83@gmail.com'
config.notify_approved_transaction = 'anil@swiftsetup.com'

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.

Ernest
  • 8,701
  • 5
  • 40
  • 51
Anil D
  • 1,989
  • 6
  • 29
  • 60
  • This is possible duplicate of http://stackoverflow.com/questions/592554/best-way-to-create-custom-config-options-for-my-rails-app and http://stackoverflow.com/questions/1450285/how-to-define-custom-configuration-variables-in-rails – Ernest Apr 11 '12 at 08:07

2 Answers2

19

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
Ernest
  • 8,701
  • 5
  • 40
  • 51
  • so, when application deployed to production, it will not pick config values form production.rb automatically? – Anil D Apr 11 '12 at 08:16
  • and the smtp settings , i have added in development.rb, those also not pick from production.rb automatically? – Anil D Apr 11 '12 at 08:19
  • @AnilD If you run Rails in produciont environment it will load configuration settings from app/environment.rb and app/environments/production.rb. So no, it will not pick settings from development. – Ernest Apr 11 '12 at 08:21
  • that's what i need, i added all settings in production too :) – Anil D Apr 11 '12 at 08:22
  • @AnilD For your custom settings and stuff like smtp you are encouraged to put those in config/initializers/. Take a look at this example: http://railscasts.com/episodes/206-action-mailer-in-rails-3?view=asciicast The /config/initializers/setup_mail.rb file will be automatically loaded. Here's where Rails.env comes in handy because you can create different setting for different environments. – Ernest Apr 11 '12 at 08:25
  • @Ernest how is this better than putting it in production.rb? – shime Apr 11 '12 at 09:48
  • @shime It's just matter of readability and organisation. The example I showed serves only as example, you could configure AM in production.rb too, but your custom stuff should definatetly go under initializers. – Ernest Apr 11 '12 at 09:55
5

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)

Community
  • 1
  • 1
Vik
  • 5,931
  • 3
  • 31
  • 38
  • Vik, when application deployed to production, it will pick config values form production.rb automatically, right? – Anil D Apr 11 '12 at 08:08
  • yup, as per the environment it will load all the settings of your environment file , but those variable should be in all env files . – Vik Apr 11 '12 at 08:49