2012/10/23

ボタンの二度押し防止

onclick="this.disabled=true;return true;" 
HTMLに上記のような感じでonclickを追加して、あとはjqueryで下記のような感じに
 
$("form").submit(function() {
  $(":submit", this).attr("disabled", "disabled");
});

powとrvmの同居環境

バージョンが上がってpowがrvmrcを読まなくなったのでこんな感じで修正。
.powrcに記入
if [ -f "$rvm_path/scripts/rvm" ] && [ -f ".rvmrc" ]; then
  source "$rvm_path/scripts/rvm"
  source ".rvmrc"
fi

railsの複合カラムのユニーク制限

validates_uniqueness_of :hoge_id, :scope => :huga_id
って感じ余裕。

Postfixでalias設定

複数の宛先を一つのアドレスで取得するために設定
main.cf
alias_maps = hash:/etc/aliases,regexp:/etc/postfix/aliases.reg 

/etc/postfix/aliases.regで正規表現でマッチさせればOK。(ユーザー名がhoge)
/^[0-9a-z\-]+(@.*)?$/ hoge 

Rackでリダイレクト

   config.gem 'rack-rewrite'
    require 'rack/rewrite'
    config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
      if ENV['RACK_ENV'] == 'production'
       # use Rack::Rewrite do
          r301 %r{.*}, 'http://hoge.com$&', :if => Proc.new {|rack_env|
            rack_env['SERVER_NAME'] != 'hoge.com'
          }
       # end
      end
    end
config/application.rbに上記のような感じで書くとRailsのRackでリダイレクト設定ができます。Herokuを使っていると便利。

ruby on railsでgoogle analyticsのコードを本番だけで挿入

<%= render :partial => ‘layouts/ga’ if RAILS_ENV == ‘production’ %>
こんな感じでレイアウトに記入。パーシャルはapp/views/llayouts/_ga.html.erbみたいな感じでOK。

cakephpでGoogle Analyticsを本番環境だけで挿入

<?php
if (Configure::read('debug') == 0){
  echo $this->element('analytics');
}
?>

こんな感じでレイアウトに記入 実際のアナリティクスのコードはapp/views/elements/analytics.ctpに書く

cakephpのヘルパーで日付拡張

  function dateFormat($date,$format = 'Y年m月d日') {
    return date($format,strtotime($date));
  }
  function df($date,$format = 'Y年m月d日') {
    return $this->dateFormat($date,$format);
  }
  function dfh($date,$format = 'Y年m月d日 H時') {
    return $this->dateFormat($date,$format);
  }
  function dfhm($date,$format = 'Y年m月d日 H時i分') {
    return $this->dateFormat($date,$format);
  }
  function dfwy($date,$format = 'm月d日') {
    return $this->dateFormat($date,$format);
  }
  function dfwyhm($date,$format = 'm月d日 H時i分') {
    return $this->dateFormat($date,$format);
  }


app/views/helper/html.php

 こんな感じで拡張してあげると便利