カテゴリー : 2012年 12月

[Ruby on Rails] 改行コードを br タグへ変換する

Ruby on Rails で改行コードを br タグへ変換するメソッドをメモ。

simple_format だと p タグが邪魔なので、自作ヘルパーを ApplicationHelper に入れておくと幸せになれそう。

module ApplicationHelper
  # 改行コードをbrタグへ変換する
  def linebreak_to_br(text)
    text.gsub(/\r\n|\r|\n/, "<br />")
  end
end

simple_format (ActionView::Helpers::TextHelper) – APIdock はてなブックマーク - simple_format (ActionView::Helpers::TextHelper) - APIdock

Google Chrome Developer Tools入門記事まとめ

Google Chrome Developer Tools 入門記事をまとめてみました。

Google Chrome Developer Tools入門 in DevFestX Sapporo はてなブックマーク - Google Chrome Developer Tools入門 in DevFestX Sapporo

[Ruby on Rails] 現在のURLの取得方法(パラメータ付きも)

Ruby on Rails3 で 現在のURLを取得する方法を色んなパターンでメモ。

■ パラメータ付きURLをそのまま使いたいとき

url_for(params)

■ パラメーターを追加するとき

url_for(params.merge(foo: 'bar'))

■ パラメーターを削除する時は、nilにする。

url_for(params.merge(foo: nil))

参考情報

Railsで現在のURLにパラメーターを追加/削除したURLを取得 – このブログは証明できない。

Haml Tips

Ruby on Rails + Haml の Tips というかよく調べることまとめ。

■ 条件分岐によるclass名設定

#someId{class: some_condition? && 'someClass'}

■ 改行、半角空白が挿入される問題

Hamlでerbの「-%>」(直後の改行を出力しない)を実現するには? – QA@IT はてなブックマーク - Hamlでerbの「-%>」(直後の改行を出力しない)を実現するには? - QA@IT

■ ul, li の繰り返し

こんな感じの ul 内に li が3つ現れたら、次の ul へというDOMをHamlで出力するには、

<ul>
    <li>a</li>
    <li>b</li>
    <li>c</li>
</ul>
 
<ul>
    <li>d</li>
    <li>e</li>
    <li>f</li>
</ul>

in_groups や in_groups_of を使って、下記のような感じに書ける。

- @items.in_groups_of(3) do |item_group|
  %ul
    - item_group.each do |item|
      %li
        = item.name

in_groups (Array) – APIdock はてなブックマーク - in_groups (Array) - APIdock

in_groups_of (Array) – APIdock はてなブックマーク - in_groups_of (Array) - APIdock

[Capistrano] rake task 実行用レシピ

Capistrano で rake task を実行するためのレシピをメモ。

namespace :rake do
  desc "Run a task on a remote server."
  # run like: cap staging rake:invoke task=db:seed_fu
  task :invoke do
    run("cd #{deploy_to}/current; rake #{ENV['task']} RAILS_ENV=#{rails_env}")
  end
end

例えば、rake db:version を Capistrano 経由で実行したい場合は下記のような感じ。

% cap staging rake:invoke task=db:seed_fu

namespace が衝突?してしまったため、rake から rake_task へ変更

namespace :rake_task do
  desc "Run a task on a remote server."
  # run like: cap staging rake:invoke task=db:seed_fu
  task :invoke do
    run("cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec rake #{ENV['task']}")
  end
end

以上です。

【参考】ruby – How do I run a rake task from Capistrano? – Stack Overflow はてなブックマーク - ruby - How do I run a rake task from Capistrano? - Stack Overflow

[Node.js] Error: Cannot find module ‘express’ の解決方法

Node.js で Express をインストール済みなのに Error: Cannot find module ‘express’ というエラーが出るときの解決方法をご紹介します。

Express.js

続きを読む

[RSpec] テストコードでの Devise の使い方いろいろ

Ruby on Rails + RSpec でテストコードを書くときの Devise の使い方いろいろメモ。

主にここを読む。

How To: Controllers and Views tests with Rails 3 (and rspec) · plataformatec/devise Wiki はてなブックマーク - How To: Controllers and Views tests with Rails 3 (and rspec) · plataformatec/devise Wiki

■ current_user にスタブを使う

Controllerのテスト

before do
  controller.stub!(:current_user).and_return(FactoryGirl.create(:user))
end

他にも。

deviseを使ったコントローラのテスト #Rails #Ruby #devise #Rspec – Qiita はてなブックマーク - deviseを使ったコントローラのテスト #Rails #Ruby #devise #Rspec - Qiita

[Node.js] gyp ERR! configure error Error: EACCES, mkdir ‘/Users/name/.node-gyp’

================================================================================
=                                                                              =
=  Attempting to build bson c++ extension                                      =
=   Windows: no build will be attempted as binaries are prepackaged            =
=   Unix: on failure the package will still install without the C++ extension  =
=                                                                              =
================================================================================
node-gyp clean
node-gyp configure build
gyp WARN install got an error, rolling back install
gyp ERR! configure error Error: EACCES, mkdir '/Users/your_username/.node-gyp'
gyp ERR! not ok 
make: *** [node_gyp] Error 1
child process exited with code 2
$ mkdir /Users/your_username/.node-gyp
$ chmod 777 /Users/your_username/.node-gyp

[Mac] GMP を Homebrew でインストールする手順

Mac に多倍長演算ライブラリ GMP を Homebrew でインストールする手順をメモ。

% brew install gmp

・公式:The GNU MP Bignum Library はてなブックマーク - The GNU MP Bignum Library

これだけ。

[Mac] ImageMagick を Homebrew でインストールする手順

Mac で ImageMagick を Homebrew でインストールする手順をメモ。

% brew install imagemagick

というか、これだけです。