Rails 動的にメールサーバを変更してアドレスごとに異なるメールを送信する方法
Railsではメーラークラスを使ってメールを送信することができる.
メーラーの設定は一般的にconfig/environments/以下に設定ファイルを
config.action_mailer.default_url_options = { :host => "localhost", :port => 3000 } config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "smtp.gmail.com", :port => 000, :domain => 'example.com', :user_name => "メールサーバ認証用ユーザー", :password => "メールサーバ認証用パスワード", :authentication => 'plain', :enable_starttls_auto => true, }
などとして設定するのだが,単一のメールアドレスだけでなく複数のアドレスから送信したい場合があると思う.
例:このコントローラーからはAのアドレスで送って,違うコントローラーからはBのアドレスで送りたい
そんな際の実装をやってみたのでメモに残しておく.
続きを読むRails application.html.erbにて変数を使う方法
Railsにおいてapplication.html.erbや_header.html.erbにて@current_user等の変数を用いたい場面があると思う.
直感的にはapplication_controller.rbにて何かすればいいように思うが,application.html.erbに対応するアクションもわからないし,どうすればよいのか悩むと思う.
その解決法は
classApplicationController<ActionController::Base
before_filter :get_current_car
def get_current_car
@current_car=CarInfo.find(session[:car_info_id])
end
end
before_filterを使えばいい
これで_headerとかもok
こうすればすべての場面で変数を使用することができる.