Railsで綺麗なURLにしたいと思うと一つのControllerに機能が集中して困ることがあります。

/comments
/posts/1/comments
/users/1/comments
# config/routes.rb:
Foo::Application.routes.draw do
  resources :comments
  resources :posts do
    resources :comments
  end
  resources :users do
    resources :comments
  end
end

例えばこんな風にしたい時。

# app/controllers/comments_controller.rb:
class CommentsController < ApplicationController
  def index
    @comments =
      if params[:post_id]
        Post.find(params[:post_id]).comments
      elsif params[:user_id]
        User.find(params[:user_id]).comments
      else
        Comment.all
      end
  end
end

こんな風に書く?えーキモーイ。そもそもそれぞれの場合でviewが全然違うんですけどーみたいな場合。

そんなんねぇ俺の糞みたいな悩みはねぇStack Overflowさんに聞けば一発なんですよ。

Rails Namespace vs. Nested Resource - Stack Overflow

controllerのnamespaceでスッキリ書けるみたいです。

/comments
/posts/1/comments
/users/1/comments
# config/routes.rb:
Foo::Application.routes.draw do
  resources :comments
  resources :posts do
    resources :comments, controller: 'posts/comments'
  end
  resources :users do
    resources :comments, controller: 'users/comments'
  end
end
# app/controllers/comments_controller.rb:
class CommentsController < ApplicationController
  def index
    @comments = Comment.all
  end
end

# app/controllers/posts/comments_controller.rb:
class Posts::CommentsController < ApplicationController
  def index
    @comments = Post.find(params[:post_id]).comments
  end
end

# app/controllers/users/comments_controller.rb:
class Users::CommentsController < ApplicationController
  def index
    @comments = User.find(params[:user_id]).comments
  end
end
$ rake routes
(snip)
comments GET    /comments(.:format)                       comments#inde
post_comments GET    /posts/:post_id/comments(.:format)     posts/comments#index
user_comments GET    /users/:user_id/comments(.:format)     users/comments#index
(snip)

おおお、これはスッキリ!

Stack Overflow脳の恐怖。

怖話をRuby 1.9.3とRails 3.2.1にした。

アプリの動作には影響無く簡単に移行できると思いきや、shoulda-contextがrails 3.2から対応しないのでテストを全てRSpecに書き換えた。

rspecコマンド単体で実行した時とrake specした時で結果が違うのが少し気になるが・・・。

怖話リゾートバイトが途中で切れるバグを直しました。

PostgreSQLの調子で使ってたMySQLのtext型の最長を超えてるだけだった。

text: 65535Byte
mediumtext: 16777215Byte
longtext: 4294967295Byte

リゾートバイトは120KBぐらいの長編なのでmediumtextに変えて対応。

# RAILS_ROOT/db/migrate/20120127081325_alter_body_to_mediumtext_stories.rb 
class AlterBodyToMediumtextStories < ActiveRecord::Migration
  def up
    if Rails.env.production?
      execute 'ALTER TABLE stories MODIFY body mediumtext COLLATE utf8_unicode_ci;'
    end
  end

  def down
    if Rails.env.production?
      execute 'ALTER TABLE stories MODIFY body text COLLATE utf8_unicode_ci;'
    end
  end
end

超えてもエラーが出ないから気づかなかったなあ。

This week in open source — giant robots smashing into other giant robots

We have officially stopped maintaining the following open source products: limerick_rake, trout, shoulda-context, and jester.

shoulda-contextが更新終了。怖話では全部コレでテスト書いてるのに。なんてこった!U2plusは混在してたのをRSpecに統一したところで調度良かったが・・・。

This week in open source — giant robots smashing into other giant robots

Hey! shoulda-context has a maintainer! His name is Travis Jeffery (travisjeffery) and he’s got commit rights and everything! Thank you, Travis.

なんて思ってたらメンテナが見つかって復活。もう何が何やら・・・。

Mac版VirtualboxでWindows XPを動かしてます。

現在10GBのディスクを16GBにしたい。

% VBoxManage modifyhd windows_xp_ie6-disk1.vmdk --resize 16000
0%...
Progress state: VBOX_E_NOT_SUPPORTED
VBoxManage: error: Resize hard disk operation for this format is not implemented yet!

not implemented yet・・・だと・・・?

新しいディスクを作って中身をコピーならできるので新しいディスク(今度は.vdi)を作成。

% VBoxManage clonehd windows_xp_ie6-disk1.vmdk NewHardDisk1.vdi --existing
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: 0e01a2f9-eeb4-44a1-8a87-1ec508afbf9b

やったね。

Windows XPのプライマリパーティションを拡張する

これだけではWindows上に未割り当て領域が増えるだけなので「ディスクの管理」から拡張しようとしましたが、Windows XPではプライマリパーティションは拡張できない。(多分最近のWindowsはできる)

EaseUS Partition Master Homeというフリーソフトで簡単に出来ました。便利。

仮想化ソフト上だとバックアップして試すのが簡単なので安心してできますね。

怖話でエラー画面の文言を何とか仕様の日本語訳として全くの間違いとは言い切れないぐらいの範囲で怖いものにできないか挑戦してみた。

404

あなたの要求された対象が計算機上に存在しません。もしくは故意に要求を処理していませんが、その理由を明かせません。または何らかの未知の現象が発生しました。

404 Not Found - 怖話 | 怖い話がスマホでも

406

あなたの要求する種類の媒体を受け入れることができません。

406 Not Acceptable - 怖話 | 怖い話がスマホでも

422

あなたの要求に同封される構成要素が処理できない、もしくは理解できません。

422 Unprocessable Entity - 怖話 | 怖い話がスマホでも

500

計算機内部であなたの要求を妨げる予期しない原因不明の状態が発生しました。

500 Internal Server Error - 怖話 | 怖い話がスマホでも

Railsの設定ファイルを環境毎に書けるプラグインをいつも見失う。なんて名前だったっけ?

rails configとか検索し辛いワードなのでここに記す。(via @fakestarbabyさん)

railsjedi/rails_config - GitHub

SinatraやPadrinoに対応してるのにrails_configとは是如何に。

# config/deploy.rb
set :git_shallow_clone, 1

Added --depth 1 option to deploy command if use this setting.

$ defaults write NSGlobalDomain KeyRepeat -int 1

You need to reboot.

レジストリを弄らない系フリーソフトは大体NAS上に置いてます。でも実行のたびに下記が出るのがうざかった。

04_01

  1. IEを起動し「ツール」→「インターネットオプション」を選択
  2. 「セキュリティ」タブ→「ローカルイントラネット」選択
  3. 「サイト」→イントラネットのネットワークを自動的に検出する」のチェックを外す
  4. 「ほかのゾーンに指定されていない・・・」、「プロキシサーバを使用しない・・・」、「すべてのネットワークパス・・・」のチェックをオンにする

OK