カテゴリー : 2012年 6月

[Ruby on Rails] Rails.env で production/development 環境か判定する

Ruby on Rails にて production/development 環境などの判定は Rails.env で行います。

Rails.env を使った production/development 環境判定のサンプルコード

if Rails.env == 'production'
  // production
elsif Rails.env == 'staging'
  // staging
elsif Rails.env == 'development'
  // development
elsif Rails.env == 'test'
  // test
end

こういう書き方もできるみたいです。

puts "Rails.env is production" if Rails.env.production?

以上です。

[Heroku][Rails] New Relic アドオンのインストール方法 [Ceder Stack]

Heroku にて、Ruby on Rails 3.2.5 で動いているアプリに New Relic アドオンをインストールしたので手順をご紹介します。

Heroku

続きを読む

[Heroku] Rails 3.2 アプリで rake assets:precompile エラーの解決方法

Heroku にて Rails 3.2 アプリを git push すると rake assets:precompile でエラーが出たときの解決方法をご紹介します。

Heroku

続きを読む

[Ruby on Rails] Amazon SES (Simple Email Service)の設定方法

Ruby on Rails 3.2.5 にて、Amazon SES (Simple Email Service)を使ってメール送信ができるようにする設定方法をメモ。

■ config/environments/production.rb

  # Amazon SES
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { host: 'your-domain.example.com' }
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.smtp_settings = {
    :address              => "email-smtp.us-east-1.amazonaws.com",
    :port                 => 465,
    :domain               => 'your-domain.example.com',
    :user_name            => 'XXXXXXXXXXXXXXXXXX',
    :password             => 'SGNIHinBVUSHLGHVIUSHIRHIIHFIL',
    :authentication       => :plain
  }

■ config/initializers/amazon_ses.rb

module Net
  class SMTP
    def tls?
      true
    end
  end
end

■ Gemfile

# Amazon SES
gem "aws-ses", "~> 0.4.4", :require => 'aws/ses'

drewblas/aws-ses · GitHub はてなブックマーク - drewblas/aws-ses · GitHub

以上です。

【参考】

Amazon SES SMTP Emails using Rails 3.1 in Three Easy Steps | | ReadyPulse BlogReadyPulse Blog はてなブックマーク - Amazon SES SMTP Emails using Rails 3.1 in Three Easy Steps | | ReadyPulse BlogReadyPulse Blog

EOFError error trying to use Amazon SES via SMTP with Rails 3.1.3 – Stack Overflow はてなブックマーク - EOFError error trying to use Amazon SES via SMTP with Rails 3.1.3 - Stack Overflow

Amazon SES(Simple Email Service)でメールを送ってみなイカ? | Pocketstudio.jp log3 はてなブックマーク - Amazon SES(Simple Email Service)でメールを送ってみなイカ? | Pocketstudio.jp log3

Amazon SESでメールを送ったときにGmailでvia email-bounces.amazonses.comって表示されないようにする – Startup Reality はてなブックマーク - Amazon SESでメールを送ったときにGmailでvia email-bounces.amazonses.comって表示されないようにする - Startup Reality

Amazon’s Simple Email Service on rails using aws-ses and devise – Tee Parham’s posterous はてなブックマーク - Amazon's Simple Email Service on rails using aws-ses and devise - Tee Parham's posterous

一番いいメール配信を頼む « サーバーワークス エンジニアブログ はてなブックマーク - 一番いいメール配信を頼む « サーバーワークス エンジニアブログ

[SSH] 秘密鍵を Dropbox にバックアップしつつ、シンボリックリンクを張って使えるようにする設定

ssh の秘密鍵を Dropbox にバックアップしつつ、シンボリックリンクを張って使えるようにする手順をご紹介します。

Dropbox へバックアップ

cp ~/.ssh/* ~/Dropbox/ssh/

シンボリックリンク作成

rm -rf ~/.ssh/
ln -s ~/Dropbox/ssh/ ~/.ssh
chmod 600 ~/Dropbox/ssh/id_rsa

以上、.ssh ファイルを管理していきたい現場からお送りしました。

[Ruby on Rails] ActiveRecord で行ロックをかける方法(楽観的ロック、悲観的ロック)

Ruby on Rails にて、ActiveRecord で行ロックをかける方法を調べたので、参考にした記事をメモ。

ロックの方法は、大きく分けて「楽観的ロック」と「悲観的ロック」の2つがあります。

■ 楽観的ロック

ずくなし。 : Ruby on Railsで楽観的ロックをかける – livedoor Blog(ブログ) はてなブックマーク - ずくなし。 : Ruby on Railsで楽観的ロックをかける - livedoor Blog(ブログ)

テストの実行環境の違いを意識する – ザリガニが見ていた…。 はてなブックマーク - テストの実行環境の違いを意識する - ザリガニが見ていた...。

■ 悲観的ロック

Railsで悲観的ロックできないの? – LazyLoadLife はてなブックマーク - Railsで悲観的ロックできないの? - LazyLoadLife

■ 両方(楽観的ロック・悲観的ロック)

ActiveRecordで行ロックをかける方法 | KRAY Inc はてなブックマーク - ActiveRecordで行ロックをかける方法 | KRAY Inc

楽観的ロックと悲観的ロック – Super Neurotic Junction はてなブックマーク - 楽観的ロックと悲観的ロック - Super Neurotic Junction

以上です。

[Heroku] heroku-postgresql:dev の設定方法

Heroku にて、従来のshared-database から新しくなった heroku-postgresql の dev をインストールしました。

Heroku

続きを読む

[Ruby on Rails] Devise のログイン処理のエラーメッセージを表示する方法

Ruby on Rails にて、Devise のログイン処理のエラーメッセージを表示させるように、下記のコードを追加しました。

■ app/views/devise/sessions/new.html.haml

  -if alert
    #error_explanation
      %h2 Please check messages below:
      %ul
        %li=alert

以上です。

【参考】

Rails – Devise – Error messages when signing in? – Stack Overflow はてなブックマーク - Rails - Devise - Error messages when signing in? - Stack Overflow

[Facebook] ページタブアプリ(iframe) でリダイレクト(redirect)できない問題と解決方法

Facebook のページタブアプリ(iframe) にて、リダイレクト(redirect)処理を実行すると、画面が真っ白になって何も表示されない問題でハマりました。

■ サーバーの request/response ログ

HTTP/1.1 302 Moved Temporarily
P3P: CP=”IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT”
Location: https://www.facebook.com/pages/My-Page/xxxxxxxxxxxx?sk=app_xxxxxxxxxx
Content-Type: text/html; charset=utf-8
X-UA-Compatible: IE=Edge
Cache-Control: no-cache
X-Request-Id: 8700a35ce62854a9d99abc225bbb9903
X-Runtime: 0.002538
Connection: close
Server: thin 1.3.1 codename Triple Espresso

<html><body>You are being <a href="https://www.facebook.com/pages/My-Page/xxxxxxxxxxxx?sk=app_xxxxxxxxxx">redirected</a>.</body></html>

原因は、ifranme 内からリダイレクトを行なっているせいでした。

a タグに、target=”_top” を指定して、フレーム解除してやればOKです。

■ エラーで動かない例

<a class="btn" href="https://graph.facebook.com/oauth/authorize?client_id=xxxxxxxxxxxxxxx&redirect_uri=https%3A%2F%2Flocalhost%3A3443%2Fcallback&scope=email">Facebook Auth</a>

■ 解決した例(target=”_top” を追加してる)

<a class="btn" href="https://graph.facebook.com/oauth/authorize?client_id=xxxxxxxxxxxxxxx&redirect_uri=https%3A%2F%2Flocalhost%3A3443%2Fcallback&scope=email" target="_top">Facebook Auth</a>

以上です。

[Ruby on Rails] Devise で 保存失敗時に例外を発生させる update_without_password! メソッドを作成しました

Ruby on Rails の Devise にて、保存失敗時に例外を発生させる update_without_password! メソッドを作成しました。

Devise で定義されている update_without_password メソッドでは、保存処理の成功・失敗によって true, false が帰ってきます。

例外を投げる update_without_password! メソッドも定義されているのかと思いきや、無かったので作成しました。

class User < ActiveRecord::Base
  def update_without_password!(params, *options)
 
    if params[:password].blank?
      params.delete(:password)
      params.delete(:password_confirmation) if params[:password_confirmation].blank? 
    end
 
    clean_up_passwords
    update_attributes!(params, *options)
  end
end

以上です。

・参考:
deviseで現在のパスワード無しでuserを更新する – komagata
はてなブックマーク - komagata