<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>imishin.com &#187; RubyOnRails</title>
	<atom:link href="http://blog.imishin.com/category/rubyonrails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.imishin.com</link>
	<description></description>
	<lastBuildDate>Thu, 26 Aug 2010 23:06:43 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/category/rubyonrails/feed/" />
		<item>
		<title>ActionMailerJa の不具合を修正しました</title>
		<link>http://blog.imishin.com/2009/08/18/actionmailer-ja-%e3%81%ae%e4%b8%8d%e5%85%b7%e5%90%88%e3%82%92%e4%bf%ae%e6%ad%a3%e3%81%97%e3%81%be%e3%81%97%e3%81%9f/</link>
		<comments>http://blog.imishin.com/2009/08/18/actionmailer-ja-%e3%81%ae%e4%b8%8d%e5%85%b7%e5%90%88%e3%82%92%e4%bf%ae%e6%ad%a3%e3%81%97%e3%81%be%e3%81%97%e3%81%9f/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 03:04:55 +0000</pubDate>
		<dc:creator>kusakari</dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[ActionMailer]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.imishin.com/?p=750</guid>
		<description><![CDATA[				しばらく放置気味だった ActionMailerJa の既知の不具合を修正しました。
				修正内容は次の通り。
				
				html メールの日本語が文字化けする不具合の修正。
				添付ファイル付き [...]]]></description>
			<content:encoded><![CDATA[				<p>しばらく放置気味だった <a title="ActionMailerJa" href="http://blog.imishin.com/projects/ruby/actionmailerja/">ActionMailerJa</a> の既知の不具合を修正しました。</p>
				<p>修正内容は次の通り。</p>
				<ul>
				<li>html メールの日本語が文字化けする不具合の修正。</li>
				<li>添付ファイル付きメールの日本語が文字化けする不具合の修正。</li>
				<li>「～」が文字化けする不具合の修正。</li>
				</ul>
				<p>「～」の文字化けについては <a title="beyond さん" href="http://github.com/beyond/" target="_blank">beyond</a> さんに修正頂きました。ありがとうございます！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.imishin.com/2009/08/18/actionmailer-ja-%e3%81%ae%e4%b8%8d%e5%85%b7%e5%90%88%e3%82%92%e4%bf%ae%e6%ad%a3%e3%81%97%e3%81%be%e3%81%97%e3%81%9f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/2009/08/18/actionmailer-ja-%e3%81%ae%e4%b8%8d%e5%85%b7%e5%90%88%e3%82%92%e4%bf%ae%e6%ad%a3%e3%81%97%e3%81%be%e3%81%97%e3%81%9f/" />
	</item>
		<item>
		<title>ActionMailerJa をバージョンアップしました</title>
		<link>http://blog.imishin.com/2009/04/28/actionmailer-ja-for-redmine-with-rails-2-2-2/</link>
		<comments>http://blog.imishin.com/2009/04/28/actionmailer-ja-for-redmine-with-rails-2-2-2/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 12:19:58 +0000</pubDate>
		<dc:creator>kusakari</dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[ActionMailer]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.imishin.com/?p=406</guid>
		<description><![CDATA[				ActionMailer に日本語対応と携帯テンプレート振り分け機能を追加するプラグイン ActionMailerJa をバージョンアップしました。
				といっても、Redmine + Rails 2.2.2 [...]]]></description>
			<content:encoded><![CDATA[				<p>ActionMailer に日本語対応と携帯テンプレート振り分け機能を追加するプラグイン <a href="http://github.com/kusakari/actionmailer_ja/tree/master" target="_blank">ActionMailerJa</a> をバージョンアップしました。</p>
				<p>といっても、Redmine + Rails 2.2.2 で <a href="http://github.com/kusakari/actionmailer_ja/tree/master" target="_blank">ActionMailerJa</a> を使うと日本語が文字化けする問題を　<a href="http://github.com/knu/" target="_blank">knu</a> さんが修正してくれたものを取り込んだだけです。</p>
				<p><a href="http://github.com/knu/" target="_blank">knu</a> さんありがとうございます！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.imishin.com/2009/04/28/actionmailer-ja-for-redmine-with-rails-2-2-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/2009/04/28/actionmailer-ja-for-redmine-with-rails-2-2-2/" />
	</item>
		<item>
		<title>valid_attributes プラグインを Rails 2.2 に対応しました</title>
		<link>http://blog.imishin.com/2009/03/19/valid_attributes-for-rails-2-2/</link>
		<comments>http://blog.imishin.com/2009/03/19/valid_attributes-for-rails-2-2/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 10:46:43 +0000</pubDate>
		<dc:creator>kusakari</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.imishin.com/?p=374</guid>
		<description><![CDATA[				最近、Rails 2.3 がリリースされたというのに、今さら valid_attributes プラグインを Rails 2.2 に対応しました。
				kusakari&#8217;s valid_attri [...]]]></description>
			<content:encoded><![CDATA[				<p>最近、Rails 2.3 がリリースされたというのに、今さら valid_attributes プラグインを Rails 2.2 に対応しました。</p>
				<p><a href="http://github.com/kusakari/valid_attributes/tree/master" target="_blank">kusakari&#8217;s valid_attributes at master &#8211; GitHub</a></p>
				<p>最近は iPhone アプリと Rails アプリを同時進行という感じです。</p>
				<p>特に iPhone 関係はブログに書きたいことも多いのですが、さすがにアプリをリリースするまでは書けないですね…。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.imishin.com/2009/03/19/valid_attributes-for-rails-2-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/2009/03/19/valid_attributes-for-rails-2-2/" />
	</item>
		<item>
		<title>ActionMailerJa を Rails 2.2 に対応しました</title>
		<link>http://blog.imishin.com/2009/02/25/actionmailerja-for-rails2-2/</link>
		<comments>http://blog.imishin.com/2009/02/25/actionmailerja-for-rails2-2/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 11:47:29 +0000</pubDate>
		<dc:creator>kusakari</dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.imishin.com/?p=365</guid>
		<description><![CDATA[				ActionMailer に日本語対応と携帯テンプレート振り分け機能を追加するプラグイン ActionMailerJa を Rails 2.2 に対応しました。
				Rails 2.2 以降では Action [...]]]></description>
			<content:encoded><![CDATA[				<p>ActionMailer に日本語対応と携帯テンプレート振り分け機能を追加するプラグイン ActionMailerJa を Rails 2.2 に対応しました。</p>
				<p>Rails 2.2 以降では ActionMailer でも layout が使えるようになったので、layout ファイルの携帯振り分け機能を実装しました。</p>
				<ul>
				<li><a title="ActionMailerJa" href="/products/ruby/actionmailerja/" target="_blank">ActionMailerJa</a></li>
				</ul>
				<h3>携帯振り分け機能について</h3>
				<p>携帯振り分けのファイル形式が、jpmobile 形式だと同じフォルダに大量にファイルができてしまうので、</p>
				<p>views/action/mobile/ 以下とかにする方がわかりやすくて良い気がしてきました。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.imishin.com/2009/02/25/actionmailerja-for-rails2-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/2009/02/25/actionmailerja-for-rails2-2/" />
	</item>
		<item>
		<title>redirect_to :back は DoCoMo 携帯では使えない</title>
		<link>http://blog.imishin.com/2009/01/10/redirect_to-back-jpmobile/</link>
		<comments>http://blog.imishin.com/2009/01/10/redirect_to-back-jpmobile/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 04:29:14 +0000</pubDate>
		<dc:creator>kusakari</dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.imishin.com/?p=337</guid>
		<description><![CDATA[				rails は 以下のようにすると直前のページに戻ることができます。
				
redirect_to :back
				このリダイレクト先は環境変数 HTTP_REFERER の値が使われるのですが、
			 [...]]]></description>
			<content:encoded><![CDATA[				<p>rails は 以下のようにすると直前のページに戻ることができます。</p>
				<pre name="code" class="ruby">
redirect_to :back</pre>
				<p>このリダイレクト先は環境変数 HTTP_REFERER の値が使われるのですが、</p>
				<p>DoCoMo の携帯は Referrer を送出しないため、この値が取れません。</p>
				<p>ではどうなるかというと、以下のようにエラーとなってしまいます。</p>
				<pre name="code" class="ruby">
ActionController::RedirectBackError (No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].):
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:1066:in `redirect_to'
    /app/controllers/teams_controller.rb:47:in `join'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:1166:in `send'
（省略）</pre>
				<p>ということで、携帯ページでは redirect_to :back は使わないようにするか、</p>
				<p>DoCoMo の場合だけ、別処理にした方が良さそうです。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.imishin.com/2009/01/10/redirect_to-back-jpmobile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/2009/01/10/redirect_to-back-jpmobile/" />
	</item>
		<item>
		<title>Rails 2.2 の i18n を使ってみた</title>
		<link>http://blog.imishin.com/2009/01/09/rails-2-2-i18n/</link>
		<comments>http://blog.imishin.com/2009/01/09/rails-2-2-i18n/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 08:49:32 +0000</pubDate>
		<dc:creator>kusakari</dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.imishin.com/?p=330</guid>
		<description><![CDATA[				Rails 2.2.2 で i18n を使ってみました。
				前評判はイマイチだったので、どんな感じなのかなと思っていたけど、個人的には GetText より好きです。
				
				手順としてはプロジェ [...]]]></description>
			<content:encoded><![CDATA[				<p>Rails 2.2.2 で i18n を使ってみました。</p>
				<p>前評判はイマイチだったので、どんな感じなのかなと思っていたけど、個人的には GetText より好きです。</p>
				<p><span id="more-330"></span></p>
				<p>手順としてはプロジェクトを作成して、scaffold を実行した後、以下を実行します。</p>
				<pre name="code" class="ruby">gem so -a http://gems.github.com
gem install amatsuda-i18n_generators
ruby script/generate i18n ja</pre>
				<p>ただ、これだけでは、一部問題がありエラーメッセージが日本語化されないようです。（amatsuda-i18n_generators v0.4）</p>
				<p>ということで、生成後の yml を3行消します。</p>
				<h3>config/locales/ja.yml</h3>
				<pre name="code" class="ruby">ja:
（省略）
  activerecord:
    errors:
      template:
        header:
          one:   "{{model}}にエラーが発生しました。"
          other: "{{model}}に{{count}}つのエラーが発生しました。"
-  activerecord:
-    errors:
-      template:
        body: "次の項目を確認してください。"

      messages:
（省略）</pre>
				<p>
				これで日本語化できました。簡単ですね。
				</p>
				<h3>label の日本語化</h3>
				<p>これだけでは label は日本語化されないみたいなので、label の日本語化を作ってみた。<br />
				config/initializers/libs.rb</p>
				<pre name="code" class="ruby">
require 'active_record_helper'
</pre>
				<p>lib/active_record_helper.rb</p>
				<pre name="code" class="ruby">
module ActionView
  module Helpers
    module FormHelper
      def label_with_ja(object_name, method, text = nil, options = {})
        if text.nil?
          text = I18n.t(method, :default => method, :scope => [:activerecord, :attributes, object_name])
        end
        label_without_ja(object_name, method, text, options)
      end
      alias_method_chain :label, :ja  #:nodoc:
    end
  end
end
</pre>
				<p>これで label も translation_ja.yml で指定した attributes 名が表示されます。</p>
				<h3>参考</h3>
				<ul>
				<li><a href="http://blog.dio.jp/2008/11/22/japanizing-rails-2-2-by-i18n-generators" target="_blank">Close to the EDGE Rails 2.2の I18nによる日本語化を最も簡単に行う方法</a></li>
				<li><a href="http://ukstudio.jp/2008/11/26/rails_22_i18n/" target="_blank">UK STUDIO &#8211; Rails2.2のi18n(国際化)を簡単に試してみた</a></li>
				</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.imishin.com/2009/01/09/rails-2-2-i18n/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/2009/01/09/rails-2-2-i18n/" />
	</item>
		<item>
		<title>RailsJa プラグインを label に対応＆ jpmobile を named route に対応</title>
		<link>http://blog.imishin.com/2008/12/13/rails-ja-and-jpmobile-versionup/</link>
		<comments>http://blog.imishin.com/2008/12/13/rails-ja-and-jpmobile-versionup/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 17:46:29 +0000</pubDate>
		<dc:creator>kusakari</dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.imishin.com/?p=306</guid>
		<description><![CDATA[				rails 2.2 が i18n 対応したので、RailsJa プラグインの必要性は Rails 2.1 系までだなと思いつつも、
				「Destiny Run」開発中に label がローカライズされないこ [...]]]></description>
			<content:encoded><![CDATA[				<p>rails 2.2 が i18n 対応したので、RailsJa プラグインの必要性は Rails 2.1 系までだなと思いつつも、</p>
				<p>「<a title="Destiny Run" href="http://destinyrun.com/" target="_blank">Destiny Run</a>」開発中に label がローカライズされないことに気づいて、機能追加したものを Git にコミットしました。</p>
				<h3>RailsJa</h3>
				<p><a title="RailsJa" href="http://github.com/kusakari/rails-ja/tree/master" target="_blank">http://github.com/kusakari/rails-ja/tree/master</a></p>
				<p>ついでに、jpmobile で named route が使えない問題も修正して、pull request してみました。</p>
				<p><span style="text-decoration: line-through;">急ぎで jpmobile で named route を使い方は以下からどうぞ</span>。</p>
				<h3><span style="text-decoration: line-through;">jpmobile</span></h3>
				<p><span style="text-decoration: line-through;"><a title="jpmobile" href="http://github.com/kusakari/jpmobile/tree/master" target="_blank">http://github.com/kusakari/jpmobile/tree/master</a></span></p>
				<p>本家に取り込んで頂けました。（2008/12/16）</p>
				<p>rails 2.2 の i18n な機能はまだ試せてないので、そのうち試したいな。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.imishin.com/2008/12/13/rails-ja-and-jpmobile-versionup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/2008/12/13/rails-ja-and-jpmobile-versionup/" />
	</item>
		<item>
		<title>restful-authentication プラグインを使ってログインアプリケーション作る（2）</title>
		<link>http://blog.imishin.com/2008/10/07/rails-with-restful-authentication-2/</link>
		<comments>http://blog.imishin.com/2008/10/07/rails-with-restful-authentication-2/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 02:10:37 +0000</pubDate>
		<dc:creator>kusakari</dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[restful-authentication]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.imishin.com/?p=238</guid>
		<description><![CDATA[				
				前回作った todo アプリケーションにメールアドレス変更機能と、エラーメッセージ・メールの日本語化を追加します。
				
				メールアドレス変更機能
				change_email プラグイン [...]]]></description>
			<content:encoded><![CDATA[				<p><a href="http://blog.imishin.com/wp-content/uploads/2008/10/todos_ja.png"><img class="alignnone size-medium wp-image-242" title="todos_ja" src="http://blog.imishin.com/wp-content/uploads/2008/10/todos_ja-300x240.png" alt="" width="300" height="240" /></a></p>
				<p><a href="http://blog.imishin.com/2008/09/14/rails-with-restful-authentication-on/">前回</a>作った todo アプリケーションにメールアドレス変更機能と、エラーメッセージ・メールの日本語化を追加します。</p>
				<p><span id="more-238"></span></p>
				<h3>メールアドレス変更機能</h3>
				<p>change_email プラグインをインストールします。</p>
				<pre class="prettyprint">ruby script/plugin install git://github.com/kusakari/change_email.git</pre>
<p>依存しているプラグインをインストールします。（valid-attributes）</p>
<pre class="prettyprint">ruby script/plugin install git://github.com/kusakari/valid_attributes.git</pre>
				<p>メールアドレス保存用のコードを自動生成します。</p>
				<pre class="prettyprint">ruby script/generate change_email email user</pre>
<p>migrate します。</p>
<pre class="prettyprint">rake db:migrate</pre>
				<p>これで、以下の URL からメールアドレスを変更することができます。（ログイン後のみ）</p>
				<blockquote><p>http://localhost:3000/change_email</p></blockquote>
				<h3>エラーメッセージの日本語化</h3>
				<p>RailsJa プラグインをインストールします。</p>
				<pre class="prettyprint">ruby script/plugin install git://github.com/kusakari/rails-ja.git</pre>
<p>次に、app/models/todo.rb を変更します。</p>
<p>以下のようにして、@@humanized_attribute_names にアトリビュートごとの名称を設定します。</p>
<p>human_model_name は日本語モデル名を返すようにします。</p>
<pre class="prettyprint">class Todo &lt; ActiveRecord::Base
  validates_presence_of     :name
  validates_length_of       :name,    :within =&gt; 1..100
  class &lt;&lt; self
    @@humanized_attribute_names = {
      "name" =&gt; "タスク名",
    }
    def human_model_name
      "Todo"
    end
  end
end</pre>
				<h3>メールの日本語対応</h3>
				<p>ActionMailerJa プラグインはインストールするだけで日本語対応できる ActionMailer 拡張です。</p>
				<pre class="prettyprint">ruby script/plugin install git://github.com/kusakari/actionmailer-ja.git</pre>
<p>ActionMailerJa はメールヘッダの Subject, From, Cc, Recipients の日本語化にも対応しているため、</p>
<p>例えば、app/models/user_mailer.rb の場合、以下のように変更します。</p>
<pre class="prettyprint">class UserMailer &lt; ActionMailer::Base
  def signup_notification(user)
    setup_email(user)
    @subject    += '新しいアカウントを有効にして下さい'

    @body[:url]  = "#{$SERVICE_URL}/activate/#{user.activation_code}"

  end

  def activation(user)
    setup_email(user)
    @subject    += 'アカウントが有効になりました'
    @body[:url]  = "#{$SERVICE_URL}"
  end

  protected
    def setup_email(user)
      @recipients  = "#{user.email}"
      @from        = "TODOアプリケーション &lt;#{$ADMIN_EMAIL}&gt;"
      @subject     = "[YOURSITE] "
      @sent_on     = Time.now
      @body[:user] = user
    end
end</pre>
				<p>以上でメールアドレスの変更機能と、エラーメッセージ・メールの日本語対応ができました。</p>
				<p>僕が現時点で Ruby on Rails を選択するのは、他のフレームワークでは、</p>
				<p>ここまでの機能を同じくらい高速で汎用的に開発することはできないと思っているからです。</p>
				<p>これはもちろん Ruby という言語によるものが大きいと思います。</p>
				<p>ということで Ruby on Rails のメリットを最大限活かせるように、プラグインやジェネレータを積極的に作っていきたいと思います。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.imishin.com/2008/10/07/rails-with-restful-authentication-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/2008/10/07/rails-with-restful-authentication-2/" />
	</item>
		<item>
		<title>restful-authentication プラグインを使ってログインアプリケーション作る（1）</title>
		<link>http://blog.imishin.com/2008/09/14/rails-with-restful-authentication-on/</link>
		<comments>http://blog.imishin.com/2008/09/14/rails-with-restful-authentication-on/#comments</comments>
		<pubDate>Sun, 14 Sep 2008 14:39:47 +0000</pubDate>
		<dc:creator>kusakari</dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[restful-authentication]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.imishin.com/?p=140</guid>
		<description><![CDATA[				
				数回に分けてログインアプリケーションを一通り作るためのまとめを書いてみます。
				
				Project の作成
				今回は何でもいいので、とりあえず todo を作成してみます。
				r [...]]]></description>
			<content:encoded><![CDATA[				<p><a href="http://blog.imishin.com/wp-content/uploads/2008/10/todos.jpg"><img class="alignnone size-medium wp-image-236" title="todos" src="http://blog.imishin.com/wp-content/uploads/2008/10/todos-300x131.jpg" alt="" width="300" height="131" /></a></p>
				<p>数回に分けてログインアプリケーションを一通り作るためのまとめを書いてみます。</p>
				<p><span id="more-140"></span></p>
				<h2>Project の作成</h2>
				<p>今回は何でもいいので、とりあえず todo を作成してみます。</p>
				<pre class="prettyprint">rails todo</pre>
<h2>Plugin Install</h2>
<p>必要なプラグインをあらかじめインストールしておきます。</p>
<h3>restful-authentication</h3>
<pre class="prettyprint">ruby script/plugin install git<span class="synIdentifier">:/</span>/github.com/technoweenie/restful-authentication.git</pre>
				<h3>aasm</h3>
				<pre class="prettyprint">gem sources -a http://gems.github.com
gem install rubyist-aasm</pre>
				<h3>forgot_password</h3>
				<pre class="prettyprint">ruby script/plugin install git://github.com/greenisus/forgot_password.git</pre>
<h2>Generate</h2>
<p>sessions_controller と user モデルを作成します。</p>
<pre class="prettyprint">ruby script/generate authenticated user sessions --include-activation --aasm</pre>
				<p>passwords_controller と password モデルを作成します。</p>
				<pre class="prettyprint">ruby script/generate forgot_password password user</pre>
<h2>その他修正箇所</h2>
<p>このままでは動作しないので、aasm 関係を読み込むように修正します。</p>
<p>vendor/plugins/restful-authentication/init.rb</p>
<pre class="prettyprint">require File.dirname(__FILE__) + '/lib/authentication'
require File.dirname(__FILE__) + '/lib/authentication/by_password'
require File.dirname(__FILE__) + '/lib/authentication/by_cookie_token'
+require 'aasm'
+require File.dirname(__FILE__) + '/lib/authorization/aasm_roles'</pre>
				<p>アクティベーション用の route を追加。<br />
				config/routes.rb</p>
				<pre class="prettyprint">（省略）
  map.activate '/activate/:activation_code', :controller =&gt; 'users', :action =&gt; 'activate'
（省略）</pre>
				<p>メールの設定をします。</p>
				<p>config/environment.rb</p>
				<pre class="prettyprint">（省略）
Rails::Initializer.run do |config|
（省略）
  config.active_record.observers = :user_observer

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address =&gt; 'SMTP サーバ',
    :port =&gt; 587, # or 25
    :domain =&gt; 'ドメイン',
    :authentication =&gt; :login,
    :user_name =&gt; 'SMTP AUTH ユーザーID',
    :password =&gt; 'SMTP AUTH パスワード'
  }
end

$ADMIN_EMAIL = '管理者のメールアドレス'
$SERVICE_URL = 'http://localhost:3000'</pre>
				<p>メールアドレスと URL を設定します。</p>
				<p>app/models/user_mailer.rb</p>
				<pre class="prettyprint">class UserMailer &lt; ActionMailer::Base
  def signup_notification(user)
    setup_email(user)
    @subject    += 'Please activate your new account'

    @body[:url]  = "#{$SERVICE_URL}/activate/#{user.activation_code}"

  end

  def activation(user)
    setup_email(user)
    @subject    += 'Your account has been activated!'
    @body[:url]  = "#{$SERVICE_URL}"
  end

  protected
    def setup_email(user)
      @recipients  = "#{user.email}"
      @from        = $ADMIN_EMAIL
      @subject     = "[YOURSITE] "
      @sent_on     = Time.now
      @body[:user] = user
    end
end</pre>
				<p>app/models/password_mailer.rb</p>
				<pre class="prettyprint">class PasswordMailer &lt; ActionMailer::Base

  def forgot_password(password)
    setup_email(password.user)
    @subject    += 'You have requested to change your password'
    @body[:url]  = "#{$SERVICE_URL}/change_password/#{password.reset_code}"
  end

  def reset_password(user)
    setup_email(user)
    @subject    += 'Your password has been reset.'
  end

  protected
    def setup_email(user)
      @recipients  = "#{user.email}"
      @from        = $ADMIN_EMAIL
      @subject     = "[YOURSITE] "
      @sent_on     = Time.now
      @body[:user] = user
    end
end</pre>
				<p>今回はすべてのコントローラのアクションを認証対象とするため、</p>
				<p>application.rb で AuthenticatedSystem を読み込み、login_required を設定します。</p>
				<p>app/controllers/application.rb</p>
				<pre class="prettyprint">class ApplicationController &lt; ActionController::Base

（省略）
  include AuthenticatedSystem

  before_filter :login_required

end</pre>
				<p>認証が必要ないアクションは except に指定します。</p>
				<p>app/controllers/users_controller.rb</p>
				<pre class="prettyprint">class UsersController &lt; ApplicationController
  before_filter :login_required, :except =&gt; [ :new,
                                              :create,
                                              :activate,
                                              :suspend,
                                              :unsuspend,
                                              :destroy,
                                              :purge,
                                            ]

（省略）

end</pre>
				<p>sessions_controller は認証が必要ないため、skip_before_filter を設定します。</p>
				<p>app/controllers/sessions_controller.rb</p>
				<pre class="prettyprint">class SessionsController &lt; ApplicationController
  skip_before_filter :login_required

（省略）

end</pre>
				<h2>起動準備</h2>
				<p>config/database.yml を設定して下さい。</p>
				<p>todo scaffold を作成。</p>
				<pre class="prettyprint">ruby script/generate scaffold todo name:string</pre>
<p>app/views/layouts/todo.html.erb を application.html.erb に変更します。</p>
<p>データベースとテーブルの作成。</p>
<pre class="prettyprint">rake db:create

rake db:migrate</pre>
				<p>ここまで設定することで、httpd を起動すると以下の動作確認することができます。</p>
				<ul>
				<li>ユーザー登録</li>
				<li>ログイン</li>
				<li>ログアウト</li>
				<li>パスワードリセット</li>
				<li>アクティベーション（メール、パスワードリセット）</li>
				</ul>
				<p>httpd は以下のコマンドで起動できます。</p>
				<pre class="prettyprint">ruby script/server</pre>
<p>動作確認のための URL は以下の通り。</p>
<blockquote><p># 会員登録<br />
htt://localhost:3000/signup<br />
# ログイン<br />
htt://localhost:3000/login<br />
# ログアウト<br />
htt://localhost:3000/logout<br />
# todo 一覧<br />
htt://localhost:3000/todos</p></blockquote>
<p>次回はこの todo にメールアドレス変更機能を追加し、日本語化してみます。</p>
<h2>参考</h2>
<p><a href="http://d.hatena.ne.jp/zariganitosh/20080903/1220412035" target="_blank">aasmとは何か？acts_as_state_machineとの違いは？</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.imishin.com/2008/09/14/rails-with-restful-authentication-on/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.imishin.com/2008/09/14/rails-with-restful-authentication-on/" />
	</item>
	</channel>
</rss>
