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

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

2012/07/10

CakePHPのモデルキャッシュ

新しくカラムを追加した場合、本番環境でのみ更新されないということがある。debug=0では999日間モデルキャッシュが効いてるので削除してあげましょうというお話。

http://pplace.jp/2011/06/158/

2012/07/09

CakePHPでQdmailを使う場合の修正

エラー文

Fatal error: Call to undefined method View::renderElement() in /***/app/controllers/components/qdmail.php on line 3823

エラー分の該当箇所を下記に修正



$content = $view->element( $this->view_dir . DS . $type . DS . $this->template , 
  array('content' => $content ) , true );



http://www.msng.info/archives/2011/01/using-qdmail-component-on-cakephp-1-3.php

JqueryのCDNが落ちてた場合の対処法

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="/サーバーURL/jquery.js"><\/script>')</script>


こんな感じで対処できる

iOSでデフォルトで動いてるプロセスリスト

Launchd: takes over many tasks from cron, xinetd, mach_init, and init, which are UNIX programs that traditionally have handled system initialization, called systems scripts, run startup items, and generally prepared the system for the user. (do not close)
TQServer: Net Long Company PC Suit daemon (recommend not to close it)
BTServer: Bluetooth Service (BlueTooth) (in my environment with the dock, turn it off iphone not responding)
CommCenter: Communications Center (phone system) (do not close)
configd: to automatically configure and maintain the network (do not close)
cron: regularly scheduled command or script execution (alarm clock might use it, recommend not to close it)
mDNSResponder: Multicast-DNS Responder daemon. (Do not turn off)
lockdownd: so that iPhone can use other SIM card (do not close)
ptpd: the process of connecting itunes (do not close)
fitx: WeFIT Input Method (not recommended to be closed)
mediaserverd: (system sounds) (do not close)
notifyd: inter-process communication (do not close)
SpringBoard: Springboard is no better explanation in English, if you used the installer or ibrickr install a third-party software, you will find the middle of the screen there is a circular symbol loader, and then immediately return to the standby screen iPhone , then this is a Springboard restart the process (do not close)
MobilePhone: I need not explain this right (do not close)
sshd: ssh daemon (you can close it)
crashreporterd: test application crashes the daemon. (Recommend to close)
dock: dock the software process (you decide to use or not)
iapd: ipod is the iphone and other Apple products using a communication protocol, the purpose is to allow other third-party devices such as communication equipment and iphone. (Recommended closure)
syslogd: recording system error logs and status messages (recommend to close)
: time to refresh the file system cache to prevent data loss caused by system crash (recommend to close). If you want to manually sync the file system cache, in text mode (ssh to connect to the iphone), implementation of the sync command.

VirtualBoxのCentOSでCPU使用率が上がる問題

/etc/grub.conf を下記のように編集


title CentOS (2.6.18-194.3.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-194.3.1.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet divider=10
        initrd /initrd-2.6.18-194.3.1.el5.img


http://digitalbox.jp/happy-go-lucky-computing/centos/howto-decrease-cpu-utilization-of-centos-on-virtualbox/

CakePHP1.3.10のテストのエラー対応


# /cake/tests/lib/reporter/cake_html_reporter.php
function CakeHtmlReporter($charset = 'utf-8', $params = array()) {
-    $params = array_map(array($this, '_htmlEntities'), $params);
-    $this->CakeBaseReporter($charset, $params);
+    $this->CakeBaseReporter($charset, $params);
+    $params = array_map(array($this, '_htmlEntities'), $params);
}


1.3.7では修正されたみたいだけど1.3.10ではまた戻っていたので対応

http://blog.longkey1.net/archive/931

Apacheがログローテートのときに落ちる問題

restartをreloadに変更して対処


vi /etc/logrotate.d/httpd/var/log/httpd/*log
{
 missingok
 notifempty
 sharedscripts
 postrotate
  #/bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
  /sbin/service httpd reload > /dev/null 2>/dev/null || true
 endscript
}

http://life1204.blogspot.jp/2008/11/httpdlogrotatehttpd.html

HTML5のcanvasで画像データの読み書き


Canvasからbase64エンコーディングされた画像データを取得する方法
var canvas = document.getElementById("myCanvas"); 
var base64 = canvas.toDataURL(); 

// LocalStorageに保存する 

window.localStorage.setItem("saveKey", base64);
LocalStorageに保存したデータをCanvasに読み込む方法
// LocalStroageからデータを取得する
var base64 = window.localStorage.getItem("saveKey");

// Imageオブジェクトを作成し、src属性にデータを設定する
var image = new Image();
image.src = base64;
image.onload = function(){

  // 画像の読み込みが終わったら、Canvasに画像を反映する。
  canvas.drawImage(image, 0, 0);
}

herokuで独自ドメインを使う

Custom Domain Add Onを入れてドメインを指定

DNSサイドでは下記を登録する

75.101.163.44
75.101.145.87
174.129.212.2
https://devcenter.heroku.com/articles/custom-domains#dns_setup

rvmとcapistranoを同時に使う時のメモ

Gemfileに追加


gem 'rvm-capistrano'

deproy.rbに追加
set :rvm_ruby_string, 'ree@rails3'                     # Or:
#set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"") # Read from local system

require "rvm/capistrano"                               # Load RVM's capistrano plugin.

https://rvm.io/integration/capistrano/

2012/04/17

bundleでpgをインストールするときのエラー対応

bundleでpgをインストールする時にmacのローカル環境ではpostgreSQLのconfign指定してあげる必要がある。Mac PortsでpostgreSQLを入れている場合下記のような感じで指定できる。

bundle config build.pg --with-pg-config=/opt/local/lib/postgresql91/bin/pg_config

postfixでqdmail.phpを使う場合

エラーを吐くので1186行目をコメントアウトする

//If(false !== @system($sendmail_path.' -d0.1 < /dev/null > /dev/null',$ret)){
//  if(is_array($ret)){
//    $ret = reset($ret);
//  }
//  $code = (int) substr($ret,0,3);
//  if( 100 === $code || 111 === $code){
//    $this->is_qmail = true;
//  }
//}

http://hal456.net/qdmail/support#c303

caekephpのauthコンポーネントで削除フラグ対応

下記のようにuserScopeで定義してあげる

$this->Auth->userScope = array('Users.delete' => NULL);

gitでsslのチェックを飛ばす

vundleなどでgithubからpluginを持ってきたいときに、環境によってsslが通らない時があるので一時的にsslのチェックを飛ばす。

export GIT_SSL_NO_VERIFY=true

読み込み専用のファイルをviで保存する方法

こんな感じ
w !sudo tee %


迷惑メールに分類されないためにする方法メモ

DNS逆引き設定
SPFの設定
SPAMメールのブラックリストに対する解除申請

rvmをupdateしたらpowが.rvmrcを読まなくなった

https://github.com/37signals/pow/issues/271
rvm を1.10.3より新しバージョンにしたらpowが動かなくなった。原因としてはpowが.rvmrcを読めなくなっているから。解決策としてはrvmのバージョンを下げるか、.powenvで環境を設定してあげる。

rvmのバージョンを下げる
rvm get 1.10.3

.rvmrcから.powenvを作る
rvm env . -- --env > .powenv

rubyの新しいパッケージが使いたいので後者の方で対応しました。