Kinopyo Blog

プログラマとしてRuby, Rails, iPhone, iPad,Macなどなどと向き合う日々のログポース

Posts Tagged ‘rails’

Rails3.1rcをHerokuで動かす

2011年06月17日

この前Herokuのドキュメントを見ましたが、まだRails3.0.7しか正式にサポートされているようです。でも実はRails 3.1rcもherokuで実行できますよ。

### in Terminal
# create rails app
rails new test_app

############ start: edit Gemfile ##############
#wrap sqlite3 in
group :test, :development do
gem 'sqlite3'
end

# add thin server
gem 'thin'

#add the rubyracer for heroku
group :production do
  gem 'therubyracer-heroku', '0.8.1.pre3'
  gem 'pg'
end

############ end: edit Gemfile ##############

### in Terminal
# bundle install
bundle

# git initialize
git init .

# add files
git commit -am "initial commit"

#create app on heroku
heroku create --stack cedar

# push app to heroku
git push heroku master

# test it
heroku open

view raw Gemfile This Gist brought to you by GitHub.

上記snippetはgistで保管しJavaScriptで表示しています。RSSでご覧の方は見れないと思います。お手数ですが、直接アクセスしてください。

Rails3.1.0 beta1のインストールと、undefined method ‘context’ for #Sprockets::Environment:のトラブル

2011年05月19日

Rails3.1.0 beta1をインストールするときにあったトラブルです。

インストール

// 3.1.0 betaに更新
sudo gem install rails --pre

// バージョン確認
rails -v

// ダミープロジェクト作成
rails new railsfoo

// 必要なGemをインストール
cd railsfoo
bundle install

// サーバ起動
rails s

トラブル

ここまで順調でしたが、http://127.0.0.1:3000にアクセスしたときにundefined method `context’ for #のトラブルに会いました。


どうやらこのSprocketsというのがポイントのようです。

ぐぐってみたらgithubでこのようなコメントがありました。

Just to save someone else the 2 minutes waiting for a bundle update sprockets that will fail, beta.2 is required by rails 3.1.0beta1. You want gem ‘sprockets’, ’2.0.0.beta.2′ in your Gemfile.

https://github.com/rails/rails/issues/453

のようでGemfileに下記を追記し再度bundle update。

gem 'sprockets', '2.0.0.beta.2'

解決〜

Error installing rails bundler requires RubyGems version >= 1.3.6

2011年03月07日

sudo gem install railsでこんなエラーが出ちゃいました。

Error installing rails bundler requires RubyGems version >= 1.3.6

解決策は

sudo gem update --system

pdating RubyGems
Updating rubygems-update
Successfully installed rubygems-update-1.6.1
Updating RubyGems to 1.6.1
Installing RubyGems 1.6.1
RubyGems 1.6.1 installed

=== 1.6.1 / 2011-03-03

Bug Fixes:

* Installation no longer fails when a dependency from a version that won't be
  installed is unsatisfied.
* README.rdoc now shows how to file tickets and get help.  Pull Request #40 by
  Aaron Patterson.
* Gem files are cached correctly again.  Patch #29051 by Mamoru Tasaka.
* Tests now pass with non-022 umask.  Patch #29050 by Mamoru Tasaka.

------------------------------------------------------------------------------

RubyGems installed the following executables:
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/gem

を実行した後にsudo gem install railsでrailsをインストールすればOKです。

Cucumberのいいところをこう語った:Delay The Solution

2011年02月04日

全文:

Cucumber vs. Steak – Adventures with Ruby

Also, the natural language is closer to your understanding of the problem you’re trying to solve. When I write in code, my programmer side becomes active. My programmer side is focussed on solutions. But I want to delay the solution as long as I can, until we have a decent understanding of the problem. So by not writing in code, you can focus more on the problem, rather than on the solution.

ここの「ソリューションをディレー」するところに共感しました。

Cucumberでは自然言語で書けるため、実装から離れた視点でいろんな「Problem」にぶつかりやすい!というのが最もの魅力ですね。

時には本当に実装から離れてプロデューサの目でやりたいことをを考えたいです。そうできないと、いつも「ソリューション」だけに力入れて考えて、もっと重要な「目的」を失うかもしれませんと思います。

もしSteakのような実装の観点から書いたテストコードだとnullとかblankとか、書いてるうちに視野がどんどん狭くなりがちかなと。もちろんそういうテストも重要ですが、「そもそもこんな仕様で大丈夫?ここで何をすべきか?」のような問題にぶつかるためには、やはりCucumberのような自然言語が有利ですね。

あ~いつRoRの仕事ができるかなー

[更新]MacでRuby on Rails開発:環境構築から

2010年11月29日

MacにはデフォルトでRubyが入ってそうです。

ターミナルを開いてrails -vを叩いたらバージョン情報が出てきました。

ruby 1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]

そしてRubyだけじゃなくRailsも入ってましてびっくりしました。

すごいですねMacは。。。

て、railsのバージョンもrails -vで確認できますが、

デフォルトのバージョンは古いそうで下記のコマンドでアップグレードできます。

sudo gem update rails

するとこんなログ情報が出ます。

Updating installed gems
Updating rails
WARNING:  Installing to ~/.gem since /Library/Ruby/Gems/1.8 and
	  /usr/bin aren't both writable.
WARNING:  You don't have /Users/zolo/.gem/ruby/1.8/bin in your PATH,
	  gem executables will not run.
Successfully installed activesupport-2.3.8
Successfully installed activerecord-2.3.8
Successfully installed rack-1.1.0
Successfully installed actionpack-2.3.8
Successfully installed actionmailer-2.3.8
Successfully installed activeresource-2.3.8
Successfully installed rails-2.3.8
Gems updated: activesupport, activerecord, rack, actionpack, actionmailer, activeresource, rails
Installing ri documentation for activesupport-2.3.8...
Installing ri documentation for activerecord-2.3.8...
Installing ri documentation for rack-1.1.0...
Installing ri documentation for actionpack-2.3.8...
Installing ri documentation for actionmailer-2.3.8...
Installing ri documentation for activeresource-2.3.8...
Installing ri documentation for rails-2.3.8...
Installing RDoc documentation for activesupport-2.3.8...
Installing RDoc documentation for activerecord-2.3.8...
Installing RDoc documentation for rack-1.1.0...
Installing RDoc documentation for actionpack-2.3.8...
Installing RDoc documentation for actionmailer-2.3.8...
Installing RDoc documentation for activeresource-2.3.8...
Installing RDoc documentation for rails-2.3.8...

多少時間がかかります。

これでMacでのRuby開発の準備は完了です。

更新

gem update railsにsudoを付けないとこんなエラーが出るかも


WARNING:  Installing to ~/.gem since /Library/Ruby/Gems/1.8 and
	  /usr/bin aren't both writable.
WARNING:  You don't have /Users/paku-k/.gem/ruby/1.8/bin in your PATH,
	  gem executables will not run.
ERROR:  Error installing rails:
	bundler requires RubyGems version >= 1.3.6

今週(10/25~10/31)新たに知ったもの

2010年11月01日

Compass

A Sass-based CSS Meta-Framework that allows you to mix and match any of the following CSS frameworks:

Compass Core

Blueprint

960

Susy

YUI

New frameworks and extensions are tracked on the wiki as they are created.

Other frameworks can be added relatively easily. Create your own!

http://github.com/chriseppstein/compass/tree#readme

http://compass-style.org/docs/

Rails Rumble

The Rails Rumble is an annual 48 hour web application development competition in which teams of skilled web application developers get one weekend to design, develop, and deploy the best web property that they can, using the power of Ruby and Rails.

http://railsrumble.com/

Monospaced font

A monospaced font, also called a fixed-pitch or non-proportional font, is a font whose letters and characters each occupy the same amount of horizontal space.[1] This contrasts to variable-width fonts, where the letters differ in size to one another.

The first monospaced typefaces were designed for typewriters, which could only move the same distance forward with each letter typed. This also meant that monospaced fonts need not be typeset like variable width fonts and were, arguably, easier to deal with.

http://en.wikipedia.org/wiki/Monospaced_font

Linuxにsqlite3-ruby gemをインストール

2010年10月15日

経緯

何も考えずにGemfileにsqlite3を定義してbundleを流したらこんなエラーが出ました。


$ gem install sqlite3-ruby

Building native extensions.  This could take a while...

ERROR:  Error installing sqlite3-ruby:

        ERROR: Failed to build gem native extension.

/home/user/.rvm/rubies/ruby-head/bin/ruby extconf.rb

checking for sqlite3.h... no

sqlite3.h is missing. Try 'port install sqlite3 +universal' or 'yum install sqlite3-devel'
extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/home/user/.rvm/rubies/ruby-head/bin/ruby --with-sqlite3-dir --without-sqlite3-dir --with-sqlite3-include --without-sqlite3-include=${sqlite3-dir}/include --with-sqlite3-lib --without-sqlite3-lib=${sqlite3-dir}/lib Gem files will remain installed in /home/user/.rvm/gems/ruby-head/gems/sqlite3-ruby-1.3.0 for inspection. Results logged to /home/user/.rvm/gems/ruby-head/gems/sqlite3-ruby-1.3.0/ext/sqlite3/gem_make.out

キーのメッセージは「sqlite3.h is missing. Try ‘port install sqlite3 +universal’ or ‘yum install sqlite3-devel’」なのでそのとおりコマンドを流しましたが、portはそのコマンドがないと、yumはsqlite3-develを見つからないと返事したんです。。。

なのでsqlite本家でソースをダウンロードしビルドしてインストールする必要があります。

sqlite3のインストール

http://www.sqlite.org/download.htmlからsqlite-amalgamation-x.x.x.tar.gz(この記事を書いてる時点では3.7.3)のTarballバージョンのファイルをダウンロードします。上から二番目のリンクです。


$ wget http://www.sqlite.org/sqlite-amalgamation-3.7.3.tar.gz

$ tar vxzf sqlite-amalgamation-3.7.3.tar.gz

$ cd sqlite-amalgamation-3.7.3

$ ./configure --prefix=/usr/local

$ make

$ sudo make install

sqlite-ruby gemのインストール

後は普通にgem installでもいいしbundle installでもOKです。


$ gem install sqlite3-ruby

Rails3 WillPaginateをカスタマイズ、CSSを適用

2010年08月25日

目標

http://mislav.uniqpath.com/will_paginate/のようなCSSを適用し、スタイリングする。

このCSSはwill_paginateのgh-pages treeからダウンロードできます。

will_paginate_style

バージョン

Rails: 3.0.0.rc1

will_paginate: 3.0.pre2

方法

例えばダウンロードしたCSSの中のdigg_paginationを適用するとします。

その場合はこの生成されたpaginateのdivのclassを”digg_pagination”に設定すればOKです。

実現する方法としては二つあります。

viewにハードコーディング
<%= will_paginate @articles, :class => "digg_pagination" %>
グローバルで設定する

例えばconfig/initializers/will_paginate.rbというファイルを作り下記のように設定します。

WillPaginate::ViewHelpers::pagination_options[:class] = "digg_pagination"

これを設定した後はサーバを再起動する必要があります。

ちょっとした問題

今の時点でのwill_paginateは該当ページの要素をemとして生成しclassは何も指定していないです。

<div class="digg_pagination">
  <span class="previous_page disabled">Previous</span>
  <em>1</em>
  <a ...>2</a>
  ......
</div>

しかしCSSではcurrentというclassでスタイリングを行ってます。なのでどうやらこのemをspanに変更しclass=”current”に変更する必要があります。方法としてはwill_paginateのあるメソッドをオーバーライドします。

will_paginateのRDOCをよく見るとPaginationListLinkRendererクラス内のpage_numberというメソッドが修正のターゲットになります。なので下記のようなクラスを作成ます。

class PaginationListLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer

  protected

  def page_number(page)
    unless == current_page
      link(page, page, :rel => rel_value(page))
    else
      # tag(:em, page)
      tag(:span, page, :class => "current")
    end
  end

end

コメントアウトされたのがそもそものコードです。

次はこのクラスをrequireしてwill_paginateのoptionに指定すればOKです。

requireが必要なクラスは私は全部config/initializers/custom_requires.rbにまとめて書いてます。

# in config/initializers/custom_requires.rb
require 'lib/pagination_list_link_renderer'

# in config/initializers/will_paginate.rb
WillPaginate::ViewHelpers.pagination_options[:render] = "PaginationListLinkRenderer"

これで完了です。

リンク

will_paginateのgithubページ

will_paginateのcssダウンロード

大変参考になった記事(英語)

Rails: requestがAjaxかどうかを判断するには

2010年08月22日

requestがajaxかどうかを判断するのは、とても簡単です。


# in controller
if request.xhr?
  ...
end

xhrはXMLHttpRequestの略称です。XHRについては下記の記事を参照してください。

今更のAjax基本:XMLHttpRequestについて

Rails: default_scopeを使った場合注意しておきたいこと

2010年08月20日

Articleモデルがあるとします。ブログの記事は普通最新の順番で並んでまのでこんなdefault_scopeを定義したかもしれません。

# in Article model
default_scope :order => "articles.created_at DESC"

これで普通にArticle.allとかfindのときはデフォルトで作成日時が最新のものから並びますが

Article.firstとArticle.lastにも影響があります。

つまり

Article.first	# <Article id:999, ...>
Article.last	# <Article id:1, ..>

考えてみれば当たり前のことですが、念のため覚えておきましょう。