カテゴリー : Ruby

[Ruby] ページング機能を追加する「will_paginate」

Rubyでページング機能を追加するGem「will_paginate」の使い方をメモ。

mislav/will_paginate はてなブックマーク - mislav/will_paginate

Home · mislav/will_paginate Wiki はてなブックマーク - Home · mislav/will_paginate Wiki

Translating will_paginate output (i18n) · mislav/will_paginate Wiki はてなブックマーク - Translating will_paginate output (i18n) · mislav/will_paginate Wiki

will_paginate | RubyGems.org | your community gem host はてなブックマーク - will_paginate | RubyGems.org | your community gem host

Rails 3 + will_paginate 3.0.pre2でAjax対応なリンクを生成する | TechRacho はてなブックマーク - Rails 3 + will_paginate 3.0.pre2でAjax対応なリンクを生成する | TechRacho

Ruby on Rails 3でwill_paginateを使ってみた。 – cys b はてなブックマーク - Ruby on Rails 3でwill_paginateを使ってみた。 - cys b

■ オプション一覧

# ==== Options
# * <tt>:class</tt> -- CSS class name for the generated DIV (default: "pagination")
# * <tt>:previous_label</tt> -- default: "« Previous"
# * <tt>:next_label</tt> -- default: "Next »"
# * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
# * <tt>:inner_window</tt> -- how many links are shown around the current page (default: 4)
# * <tt>:outer_window</tt> -- how many links are around the first and the last page (default: 1)
# * <tt>:link_separator</tt> -- string separator for page HTML elements (default: single space)
# * <tt>:param_name</tt> -- parameter name for page number in URLs (default: <tt>:page</tt>)
# * <tt>:params</tt> -- additional parameters when generating pagination links
#   (eg. <tt>:controller => "foo", :action => nil</tt>)
# * <tt>:renderer</tt> -- class name, class or instance of a link renderer (default in Rails:
#   <tt>WillPaginate::ActionView::LinkRenderer</tt>)
# * <tt>:page_links</tt> -- when false, only previous/next links are rendered (default: true)
# * <tt>:container</tt> -- toggles rendering of the DIV container for pagination links, set to
#   false only when you are rendering your own pagination markup (default: true)

will_paginate/lib/will_paginate/view_helpers.rb at master · mislav/will_paginate はてなブックマーク - will_paginate/lib/will_paginate/view_helpers.rb at master · mislav/will_paginate

will_paginateのオプション設定でpaginationの外観を変更する方法 – memo.yomukaku.net はてなブックマーク - will_paginateのオプション設定でpaginationの外観を変更する方法 - memo.yomukaku.net

will_paginateヘルパーのオプション覚書 – screw it! はてなブックマーク - will_paginateヘルパーのオプション覚書 - screw it!

国際化対応(i18n対応)

i18n対応だけど、ここで初期値をセットしておくと楽。

■ config/locales/will_paginate.ja.yml

ja:
  will_paginate:
    previous_label: "&lt;"
    next_label: "&gt;"
    page_gap: "..."

ページング出力のHTMLタグカスタマイズ

Roll your own pagination links with will_paginate – UK Specialists in Ruby on Rails, Exalead, AWS, Consultancy はてなブックマーク - Roll your own pagination links with will_paginate – UK Specialists in Ruby on Rails, Exalead, AWS, Consultancy

[Ruby on Rails] find で ActiveRecord::RecordNotFound

Rails にて find メソッドで id のみを引数で渡して、結果レコードが0件の場合、ActiveRecord::RecordNotFound の例外が発生してしまいます。

例外処理をするもよし。

rescue ActiveRecord::RecordNotFound

find_by_id を使えば、結果が0件の場合 nil が返ってくるので、find_by_id を使うもよし。

【参考】

findよりfind_by_idでActiveRecord::RecordNotFoundを回避する – パンプキンスパイスラテ はてなブックマーク - findよりfind_by_idでActiveRecord::RecordNotFoundを回避する - パンプキンスパイスラテ

[Ruby] God : The server is not available (or you do not have permissions to access it)

god の設定ファイルの記述が間違っていて、god のプロセスが立ち上がらなかった。

god が起動していないので、当たり前だが何をやっても下記のようなプロセスが起動していないエラーメッセージ。

The server is not available (or you do not have permissions to access it)

原因は、まとめて god の設定ファイルを読み込めるように下記のように書いていたけど、これではダメだった。

■ /etc/god/master.conf

load "/srv/myapp/current/config/god/*.god"

↓(修正)

load "/srv/myapp/current/config/god/resque.god"

現状、Resque のプロセスのみ監視すれば良いので、このように修正しました。

以上です。

[Ruby] Foreman の使い方参考サイトまとめ

Ruby のプロセス管理ツール Foreman の使い方の参考サイトをまとめてみました。

Github
ddollar/foreman はてなブックマーク - ddollar/foreman

マニュアルページ
foreman(1) – manage Procfile-based applications はてなブックマーク - foreman(1) - manage Procfile-based applications

僕らのRailsCasts
#281 Foreman – RailsCasts はてなブックマーク - #281 Foreman - RailsCasts

他にも。
David Dollar はてなブックマーク - David Dollar

foreman について – 君の瞳はまるでルビー – Ruby 関連まとめサイト はてなブックマーク - foreman について - 君の瞳はまるでルビー - Ruby 関連まとめサイト

[Ruby] CentOS6.3 へ Nokogiri をインストール

CentOS6.3 にて、Nokogiri をインストールしようとしたら、libxml2 が無いとエラーがでました。

■ エラーメッセージ

libxml2 is missing.
please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.

■ 解決方法

Nokogiri に必要なライブラリを yum でインストールする。

# yum -y install libxml2 libxml2-devel libxslt libxslt-devel

以上です。

【参考】Nokogiri はてなブックマーク - Nokogiri

[Ruby] sunspot_solr でインストールされる Solr のバージョン確認方法

sunspot_solr のデフォルトの Solr のバージョンは、下記のURLで確認できます。

http://localhost:8982/solr/admin/registry.jsp

以上です。

[Ruby] WARNING: Nokogiri was built against LibXML version 2.7.3, but has dynamically loaded 2.7.8

Ruby on Rails で sunspot の gem をインストール後、rails g sunspot_rails:install したら Nokogiri と LibXML のあるあるエラーが発生しました。

エラー内容

WARNING: Nokogiri was built against LibXML version 2.7.3, but has dynamically loaded 2.7.8

解決方法

下記の Stack Overflow の質問に書いてあるが、Mac OS の場合だと、Homebrew 経由で、libxml2 libxslt libiconv をインストール後、これらに Path を通して Nokogiri をインストールすればOK.

compilation – WARNING: Nokogiri was built against LibXML version 2.7.7, but has dynamically loaded 2.6.16 – Stack Overflow

libxml2 libxslt libiconv と Nokogiri のインストール方法は、下記を参考にしました。

Nokogiri

以上です。

[Apache] Passenger の Basic 認証設定

Apache + Passenger で Basic 認証を設定したのでメモ。

myuser というユーザのパスワードファイルを作成する。

# htpasswd -c /usr/local/apache2/.htpasswd myuser

Apache の設定ファイルを修正する。

# vim /usr/local/apache2/conf/extra/passenger.conf
<VirtualHost *:80>
    PassengerUserSwitching off
    PassengerDefaultUser app
 
    ServerName example.com
    DocumentRoot "/srv/example.com/current/public"
    ErrorLog "logs/example.com-error_log"
    CustomLog "logs/example.com-access_log" common
    <Directory /srv/example.com/current/public>
        AllowOverride all
        Options -MultiViews
        Order allow,deny
        Allow from all
    </Directory>
    <Location />
        AuthType Basic
        AuthName "Please enter your ID and Password"
        AuthUserFile /usr/local/apache2/.htpasswd
        Require user myuser
    </Location>
 
</VirtualHost>

Apache の再起動

# service httpd restart

以上です。

【参考】

@IT:Apacheでユーザー認証を行うには(Basic認証編) はてなブックマーク - @IT:Apacheでユーザー認証を行うには(Basic認証編)

blog.sowatchasayin: rails + passengerでbasic認証 はてなブックマーク - blog.sowatchasayin: rails + passengerでbasic認証

[Ruby] cannot load such file — ci/reporter/rake/rspec

Ruby on Rails で Capistrano でデプロイしたら、bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile の実行で下記のエラーが発生したので、解決方法をメモ。

■ エラーメッセージ

cannot load such file -- ci/reporter/rake/rspec

ci_reporter は development, test 環境のみにインストールする gem なので、Rakefile に LoadError の例外をキャッチするコードを追記して、production 環境でも動くように対応しました。

% cat Rakefile 
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
 
require File.expand_path('../config/application', __FILE__)
 
begin
  require 'ci/reporter/rake/rspec'
rescue LoadError
end
 
myapp::Application.load_tasks

以上です。

Ruby1.9.3 を CentOS6.3 にソースコードからインストールする手順メモ

Ruby1.9.3 を CentOS6.3 にソースコードからインストールする手順をメモ。

■ yum

# yum check-update
# yum update
# yum -y install openssl-devel zlib-devel readline-devel

■ yaml のインストール

# cd /usr/src/
# wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
# tar zxvf yaml-0.1.4.tar.gz 
# cd yaml-0.1.4
# ./configure |& tee configure.log
# make |& tee make.log
# make install

■ Ruby のインストール
ダウンロード – Ruby はてなブックマーク - ダウンロード

# wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p286.tar.gz
# tar zxvf ruby-1.9.3-p286.tar.gz
# cd ruby-1.9.3-p286
# ./configure \
--prefix=/usr/local \
--enable-shared \
--disable-install-doc \
--with-opt-dir=/usr/local/lib \
|& tee configure.log
# make |& tee make.log
# make install
# ruby -v

■ Gem のアップデート

# gem -v
1.8.23
# gem update --system
# gem -v
1.8.24

以上です。

【参考】

完全にメモ。CentOS 6.2 に ruby をインストール : てるてる坊主 はてなブックマーク - 完全にメモ。CentOS 6.2 に ruby をインストール : てるてる坊主