<?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/"
	>

<channel>
	<title>ハセテツラボ &#187; RubyOnRails</title>
	<atom:link href="http://tt-house.com/tag/rubyonrails/feed" rel="self" type="application/rss+xml" />
	<link>http://tt-house.com</link>
	<description>生涯一開発屋</description>
	<lastBuildDate>Fri, 03 Sep 2010 14:01:41 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>RubyonRailsでOAuth経由でTwitterのタイムラインを取得する</title>
		<link>http://tt-house.com/2010/06/rubyonrails-oauth-twitter.html</link>
		<comments>http://tt-house.com/2010/06/rubyonrails-oauth-twitter.html#comments</comments>
		<pubDate>Fri, 18 Jun 2010 03:56:23 +0000</pubDate>
		<dc:creator>ハセテツ</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://tt-house.com/?p=301</guid>
		<description><![CDATA[OAuthを使えるようになっておけば今後いろいろなマッシュアップに便利だろうと思い、試してみました。RoRで書いたのは、最近ご無沙汰過ぎてあまりに覚えてなかったのでリハビリを兼ねたわけです。 class TwitterAuth require &#8216;oauth&#8217; def self.consumer OAuth::Consumer.new( [Consumer key], [Consumer secret], :site =&#62; &#8220;http://api.twitter.com&#8221; ) end def self.request_token(_token, _secret) OAuth::RequestToken.new( consumer, _token, _secret ) end def self.access_token(_token, _secret) OAuth::AccessToken.new( consumer, _token, _secret ) end end まずは上記のクラスを作成。OAuth関連の処理はこっちにやらせます。で、次がコントローラー。 require &#8216;rubytter&#8217; class AuthController &#60; ApplicationController def index request_token = TwitterAuth.consumer.get_request_token(:oauth_callback =&#62; &#8220;http://#{request.host_with_port}/auth/callback&#8221;) session[:request_token] = request_token redirect_to request_token.authorize_url [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2010%252F06%252Frubyonrails-oauth-twitter.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22RubyonRails%E3%81%A7OAuth%E7%B5%8C%E7%94%B1%E3%81%A7Twitter%E3%81%AE%E3%82%BF%E3%82%A4%E3%83%A0%E3%83%A9%E3%82%A4%E3%83%B3%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%22%20%7D);"></div>
<p>OAuthを使えるようになっておけば今後いろいろなマッシュアップに便利だろうと思い、試してみました。RoRで書いたのは、最近ご無沙汰過ぎてあまりに覚えてなかったのでリハビリを兼ねたわけです。</p>
<blockquote><p>class TwitterAuth<br />
require &#8216;oauth&#8217;</p>
<p style="padding-left: 30px;">def self.consumer</p>
<p style="padding-left: 60px;">OAuth::Consumer.new(<br />
[Consumer key],<br />
[Consumer secret],<br />
:site =&gt; &#8220;http://api.twitter.com&#8221;<br />
)</p>
<p style="padding-left: 30px;">end</p>
<p style="padding-left: 30px;">def self.request_token(_token, _secret)</p>
<p style="padding-left: 60px;">OAuth::RequestToken.new(<br />
consumer,<br />
_token,<br />
_secret<br />
)</p>
<p style="padding-left: 30px;">end</p>
<p style="padding-left: 30px;">def self.access_token(_token, _secret)</p>
<p style="padding-left: 60px;">OAuth::AccessToken.new(<br />
consumer,<br />
_token,<br />
_secret<br />
)</p>
<p style="padding-left: 30px;">end</p>
<p>end</p></blockquote>
<p>まずは上記のクラスを作成。OAuth関連の処理はこっちにやらせます。で、次がコントローラー。</p>
<blockquote><p>require &#8216;rubytter&#8217;</p>
<p>class AuthController &lt; ApplicationController</p>
<p style="padding-left: 30px;">def index</p>
<p style="padding-left: 60px;">request_token = TwitterAuth.consumer.get_request_token(:oauth_callback =&gt; &#8220;http://#{request.host_with_port}/auth/callback&#8221;)<br />
session[:request_token] = request_token<br />
redirect_to request_token.authorize_url</p>
<p style="padding-left: 30px;">end</p>
<p style="padding-left: 30px;">def callback</p>
<p style="padding-left: 60px;">_token = session[:request_token]<br />
request_token = TwitterAuth.request_token(_token.token, _token.secret)<br />
access_token = request_token.get_access_token(<br />
{},<br />
:oauth_token =&gt; params[:oauth_token],<br />
:oauth_verifier =&gt; params[:oauth_verifier]<br />
)<br />
session[:request_token]=nil<br />
_account = {<br />
:user_id =&gt; access_token.params[:user_id],<br />
:token =&gt; access_token.token,<br />
:secret =&gt; access_token.secret<br />
}<br />
session[:account] = _account<br />
redirect_to :action =&gt; &#8220;timeline&#8221;</p>
<p style="padding-left: 30px;">end</p>
<p style="padding-left: 30px;">def timeline</p>
<p style="padding-left: 60px;">_account = session[:account]<br />
token = TwitterAuth.access_token(_account[:token] , _account[:secret])<br />
_twitter = OAuthRubytter.new(token)<br />
@user_timeline = _twitter.user_timeline(_account[:user_id],:count =&gt; 100)</p>
<p style="padding-left: 30px;">end</p>
<p>end</p></blockquote>
<p>def timelineが自分のタイムラインを取ってくるところで、「:count=&gt;100」と指定しているのは「新着100件取ってきて」っていうことです。ここを指定しないでおくと規定値では２０件取得してくれます。def callbackでセッションに入れたuser_id、token、secretをデータベース等に保存しておけば次回以降はこれらだけで認証とツイートの取得ができます（というか認証自体はtokenとsecretだけでOK）。</p>
<p><a title="アプリケーション登録申請" href="http://twitter.com/apps/new" target="_blank">Twitterのアプリケーション登録申請</a>で登録する際、コールバックURLを指定しないとWebアプリケーションとして登録されません。「開発中だからURLなんて決まってないよ」っていうケースでも、適当なURLを登録しておいてください。どうせ実際のコールバックURLはRubyのスクリプトで指定しなおしています。ハセテツはここに気が付かず、そこそこの時間を浪費しました。</p>
<p>で、タイムラインを表示するviewは</p>
<blockquote><p>&lt;ul&gt;<br />
&lt;% for item in @user_timeline do -%&gt;<br />
&lt;li&gt;&lt;%= item.text %&gt;&lt;/li&gt;<br />
&lt;% end -%&gt;<br />
&lt;/ul&gt;</p></blockquote>
<p>こんな感じです。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2010/06/rubyonrails-oauth-twitter.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RubyとPythonについて考えてみました</title>
		<link>http://tt-house.com/2010/04/think-about-ruby-python.html</link>
		<comments>http://tt-house.com/2010/04/think-about-ruby-python.html#comments</comments>
		<pubDate>Tue, 27 Apr 2010 05:28:06 +0000</pubDate>
		<dc:creator>ハセテツ</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://tt-house.com/?p=278</guid>
		<description><![CDATA[ハセテツは複数の言語を状況に応じて使い分けてきましたが、最近のWeb系開発はPython（+Django）がメインになってきています。

ちょっと前までRuby（+Rails）だったのですが、いろいろと考えるところもあり、乗り換えました。その乗り換えた理由をまとめてみようと思います。

まとめてみたら「やっぱりRailsじゃね？」となるかもしれません。ｗ]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2010%252F04%252Fthink-about-ruby-python.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A8Python%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6%E8%80%83%E3%81%88%E3%81%A6%E3%81%BF%E3%81%BE%E3%81%97%E3%81%9F%22%20%7D);"></div>
<p>ハセテツは複数の言語を状況に応じて使い分けてきましたが、最近のWeb系開発はPython（+Django）がメインになってきています。</p>
<p>ちょっと前までRuby（+Rails）だったのですが、いろいろと考えるところもあり、乗り換えました。その乗り換えた理由をまとめてみようと思います。</p>
<p>まとめてみたら「やっぱりRailsじゃね？」となるかもしれません。ｗ</p>
<p>Ruby（+Rails）</p>
<ul>
<li>Rails便利すぎる。</li>
<li>コントローラとモデルが別々のファイルになってくれるのはソースが追いやすい。</li>
<li>Railsを通さない画像やCSSはpublicフォルダに置けばよいのはわかりやすい。</li>
<li>urlディスパッチャーがいまいち使いにくい。（知らないだけかも）</li>
<li>APサーバはmongrel一択？</li>
<li>VirtualHost使おうとするとApache+Passenger（mod_rails）だが、これが重い。（チューニングで速くなるのかも）</li>
<li>というか、Railsがそもそも重い。</li>
<li>ちゅーか、Rubyが重い、遅い。</li>
<li>PassengerはWindowsじゃ動かない。</li>
<li>mongrelもPassengerより激速軽快かというと、そうでもない。</li>
<li>gem便利すぎ。</li>
<li>ワンライナで書く人が多くて、Perlとおなじ匂いがする。</li>
</ul>
<p>ハセテツが使っていたRubyは1.8で、速くなったといわれる1.9には触れていないのでもしかすると古いのかもしれません。でも、Railsって1.9には対応してないですよね？（本日現在）</p>
<p>Python（+Django）</p>
<ul>
<li>Django便利すぎる。</li>
<li>日本語の書籍、情報が少なすぎる。（ハマるとキツい）</li>
<li>modelが一枚のファイルなので、大量のmodelがあると可読性が激しくダウン。</li>
<li>viewファイルをフォルダで分けたくても、アプリケーションフォルダ直下以外にviewファイル置くなら「sys.path.append」しないといけない。（違う？）</li>
<li>urlディスパッチャーのカスタマイズが超便利。</li>
<li>mod_pythonが使えるので、Windows環境だろうがLinux環境だろうが気にせずVirtualHostが使える。</li>
<li>速い。</li>
<li>日本語大変。Shift_JIS怖い。</li>
<li>easy_installでパッケージのインストールは楽だが、管理が悪夢。</li>
</ul>
<p>メリットとデメリットが入り乱れました。見難くてすいません。</p>
<p>結局フルスタックのフレームワークということで、DjangoとRailｓに関しては一長一短だと思います。個人的にはRailsの方が良くできてたかなぁと。</p>
<p>最終的に、「RubyonRailsは重い」というオチになってしまうのです。Rubyも、Railsも、です。ただ、これはRuby1.9が実は劇的に速くなっていて、1.9に対応したRailsがリリースされたらすべて解決されるのかもしれません。</p>
<p>それでも、いまのところPythonの軽快さ、シンプルさに満足しています。あと、GAEでPythonが動くのもいいですよね。</p>
<p>「Googleが導入した言語」というのがハセテツ的に琴線に触れたのかもしれません。</p>
<p>ミーハーですいません。ｗ</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2010/04/think-about-ruby-python.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>RubyonRailsでページングする方法</title>
		<link>http://tt-house.com/2009/09/pagenate_on_rubyonrails.html</link>
		<comments>http://tt-house.com/2009/09/pagenate_on_rubyonrails.html#comments</comments>
		<pubDate>Mon, 28 Sep 2009 13:38:56 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=113</guid>
		<description><![CDATA[will_paginateを利用します。とってもらくちんです。 require ...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F09%252Fpagenate_on_rubyonrails.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22RubyonRails%E3%81%A7%E3%83%9A%E3%83%BC%E3%82%B8%E3%83%B3%E3%82%B0%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%22%20%7D);"></div>
<p>will_paginateを利用します。とってもらくちんです。</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p>require &#8216;will_paginate&#8217;</p>
<p>def hoge<br />&nbsp; @items = Item.pagenate(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :conditions =&gt; ["hoge= ?", hoge],<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:page =&gt; params[:page],<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:per_page =&gt; 30)<br />end</p>
</blockquote>
<p dir="ltr">コントローラ側は以上。30レコードでページングします。find_by_sqlみたいな使い方も可能です。</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p dir="ltr">&nbsp; @items = Item.paginate_by_sql(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["select * from hoge where hoge = ?", hoge],<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:page =&gt; params[:page],<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:per_page =&gt; 30)</p>
</blockquote>
<p dir="ltr">たいして変わりません。View側も通常のActiveRecordとの違いは</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p dir="ltr">&lt;%= will_paginate @items, :prev_label =&gt; &#8216;&lt; 前&#8217;, :next_label =&gt; &#8216;次 &gt;&#8217; %&gt;</p>
</blockquote>
<p dir="ltr">こういうタグを記述しておけば勝手にページングアンカーが表示されるようになるくらいです。</p>
<p dir="ltr">:prev_labelは指定しなくてもOKです。デフォルトの文字列が表示されます。</p>
<p dir="ltr">&nbsp;</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/09/pagenate_on_rubyonrails.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyonRailsでform_tagのselect_tagを使う</title>
		<link>http://tt-house.com/2009/08/rubyonrailsform_tagselect_tag.html</link>
		<comments>http://tt-house.com/2009/08/rubyonrailsform_tagselect_tag.html#comments</comments>
		<pubDate>Tue, 25 Aug 2009 12:55:19 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=110</guid>
		<description><![CDATA[#コントローラ側@target_year_options = [2007, 2...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F08%252Frubyonrailsform_tagselect_tag.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22RubyonRails%E3%81%A7form_tag%E3%81%AEselect_tag%E3%82%92%E4%BD%BF%E3%81%86%22%20%7D);"></div>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p>#コントローラ側<br />@target_year_options = [2007, 2008, 2009]</p>
<p>#ビュー側<br />&lt;%= select_tag(&#8220;target_year&#8221;, options_for_select(@target_year_options, Time.now.year)) %&gt;</p>
</blockquote>
<p>引数は「名前、option（要素配列、selectedの値）」ですね。selectedの値は省略可能です。</p>
<p>ビュー側に</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p>&lt;%= select_tag(&#8220;target_month&#8221;, options_for_select(['01','02','03','04','05','06','07','08','09','10','11','12'], Time.now.month )) %&gt;</p>
</blockquote>
<p>って感じで直接配列を書き込むことも可能。</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p>&lt;%= select_tag(&#8220;target_month&#8221;, options_for_select([['一月',1],['二月',2],['三月',3],['四月',4]], Time.now.month )) %&gt;</p>
</blockquote>
<p>これで表示は漢字、valueが数字、っていうやり方もできます。</p>
<p>こういうことも書き溜めておかないと、必要なときになってググるはめになるんですよね。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/08/rubyonrailsform_tagselect_tag.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyonRailsでLike演算子を使ったあいまい検索をする方法</title>
		<link>http://tt-house.com/2009/07/rubyonrails-like-query.html</link>
		<comments>http://tt-house.com/2009/07/rubyonrails-like-query.html#comments</comments>
		<pubDate>Tue, 07 Jul 2009 10:38:22 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=104</guid>
		<description><![CDATA[:conditionsにクエリ文字列ごりごり書くなら別に気にしなくてもいいのです...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F07%252Frubyonrails-like-query.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22RubyonRails%E3%81%A7Like%E6%BC%94%E7%AE%97%E5%AD%90%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E3%81%82%E3%81%84%E3%81%BE%E3%81%84%E6%A4%9C%E7%B4%A2%E3%82%92%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%22%20%7D);"></div>
<p>:conditionsにクエリ文字列ごりごり書くなら別に気にしなくてもいいのですが、まぁたいていはインジェクション対策も兼ねてプレースホルダーを使うか、シンボルを使うかのどちらかでは無いでしょうか。</p>
<p>ハセテツは前者です。PHPでもPEARを使っていたので、プレースホルダーを使っていました。</p>
<p>で、Rails（ActiveRecord）でプレースホルダーを利用している状況でLike演算子を使ったあいまい検索はどうしたらよいものか、と意外にシンプルな部分で躓いたりします。キーワード検索機能を実装しない限り、Like演算子って使わないですよね。</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>:conditions =&gt; ["hoge like ?", "%#{hogeParam}%"]</p></blockquote>
<p>上記の書き方であいまい検索ができました。手抜きではあるのですが、これでhogeParamが空白文字列であっても検索はできます。nilだったらどうしようもないですけどね。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/07/rubyonrails-like-query.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyonRailsでクッキーの処理をモジュールで共通化する方法</title>
		<link>http://tt-house.com/2009/06/rubyonrails-include-cookies.html</link>
		<comments>http://tt-house.com/2009/06/rubyonrails-include-cookies.html#comments</comments>
		<pubDate>Thu, 18 Jun 2009 12:53:37 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=102</guid>
		<description><![CDATA[猛烈にはまったので、これは書き残しておかないと後々後悔すると思った次第であります...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F06%252Frubyonrails-include-cookies.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22RubyonRails%E3%81%A7%E3%82%AF%E3%83%83%E3%82%AD%E3%83%BC%E3%81%AE%E5%87%A6%E7%90%86%E3%82%92%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%E3%81%A7%E5%85%B1%E9%80%9A%E5%8C%96%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%22%20%7D);"></div>
<p>猛烈にはまったので、これは書き残しておかないと後々後悔すると思った次第であります。もしかするとハセテツがやり方を知らないだけで意外と簡単なのかもしれません。もし「いやいや、あんたが知らないだけでこうやるとラクチンだよ」という情報お持ちの方がいたら教えていただけると非常にありがたいです。</p>
<p>Railsでクッキーを扱う際、コントローラに書けば特に問題はありません。ただ、メソッドをモデルやモジュール側に書いて置くと、コールしたときに怒られます。これはApplicationControllerを継承していないからのようです。といって、他のコントローラの中にメソッドを書いておいてそれをコールしても怒られます。クッキーは「自分のトコで処理しろや」ということなのでしょうか。</p>
<p>かといって、クッキーのチェックなどなどをすべてのコントローラに書くのはDRYに反しているのでは、と悩んでおったのです。</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>module HogeModule<br />
 <br />
  def self.included(base)<br />
  base.class_eval{</p>
<p>    def set_cookies</p>
<p>      cookies[:key] = {:value =&gt; &#8220;hogehoge&#8221;, :path =&gt; &#8220;/&#8221;, :expires =&gt; Time.now + 45 }<br />
      end</p>
<p>    }<br />
  end</p>
<p>end</p></blockquote>
<p>上記のようなモジュールを用意します。これは、このモジュールがコントローラからインクルードされるとset_cookiesというメソッドがインクルードした方のコントローラ側に展開され、selfとして扱えるということです。つまり、コントローラ自身のメソッドになる、という感じです（多分）。</p>
<p>あとはコントローラからset_cookiesをコールするだけです。エラーはでず、クッキーも書き込まれます。</p>
<p>base.class_evalの中に:before_filterを書いておけば、インクルードされるたびにフィルターも実行されます。ハセテツはbefore_filterでクッキーのチェックを行い、メソッドで書き込みを行うように書きました。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/06/rubyonrails-include-cookies.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyでPOST送信して結果を受け取る</title>
		<link>http://tt-house.com/2009/06/ruby-post-data.html</link>
		<comments>http://tt-house.com/2009/06/ruby-post-data.html#comments</comments>
		<pubDate>Wed, 17 Jun 2009 11:55:46 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=101</guid>
		<description><![CDATA[RubyでHTTP経由でのXMLの受信と解析でGETする方法は書きましたが、PO...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F06%252Fruby-post-data.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A7POST%E9%80%81%E4%BF%A1%E3%81%97%E3%81%A6%E7%B5%90%E6%9E%9C%E3%82%92%E5%8F%97%E3%81%91%E5%8F%96%E3%82%8B%22%20%7D);"></div>
<p><a href="http://www.tt-house.com/2009/05/rubyhttpxml.html">RubyでHTTP経由でのXMLの受信と解析</a>でGETする方法は書きましたが、POSTまでは書いていませんでした。今回はRubyでPOSTする方法です。GETで猛烈に長いクエリをつければPOSTできなくても同じことが実現できますが、まぁそこは気にせず。</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p>require &#8216;net/http&#8217;<br />Net::HTTP.version_1_2<br />http = Net::HTTP.new(ホスト名, 80)<br />response = http.post( &#8216;/hoge.php&#8217;, &#8216;a=hoge&amp;b=hogehoge&#8217; )<br />p response.body</p>
</blockquote>
<p>これだけ。ホスト名にはhttpは付けません。ポート番号は省略しても大丈夫です。Content-Type等は必要に応じて。newするときの第三引数です。「application/x-www-form-urlencoded」でいいのかな？ハセテツはつけてませんが、きちんとPOSTできてます。あー、相手がUTF8じゃない場合とかは文字コードの指定が必要かも。</p>
<p>responseの中身を見ればHTTPステータスコードとかも入っていると思います。その辺は割愛。</p>
<p>次はファイルのアップロードのやり方なんかも調べてみましょう。使うかどうかは微妙な気もしますが。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/06/ruby-post-data.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyonRailsでクエリを直接実行したいときの方法</title>
		<link>http://tt-house.com/2009/06/rubyonrails-execute-query.html</link>
		<comments>http://tt-house.com/2009/06/rubyonrails-execute-query.html#comments</comments>
		<pubDate>Tue, 16 Jun 2009 11:44:01 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=100</guid>
		<description><![CDATA[ActiveRecord::Base.connection.execute(実...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F06%252Frubyonrails-execute-query.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22RubyonRails%E3%81%A7%E3%82%AF%E3%82%A8%E3%83%AA%E3%82%92%E7%9B%B4%E6%8E%A5%E5%AE%9F%E8%A1%8C%E3%81%97%E3%81%9F%E3%81%84%E3%81%A8%E3%81%8D%E3%81%AE%E6%96%B9%E6%B3%95%22%20%7D);"></div>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p>ActiveRecord::Base.connection.execute(実行したいクエリ)</p>
</blockquote>
<p>インデックス張りなおしたいときや更新クエリをごそっと投げたいときなんかは意外と重宝します。</p>
<p>これにコールバック関数とか組み合わせることができると便利なんですよね。あ、<a href="http://www.tt-house.com/2009/01/rubyonrailsbackgroundrb.html">BackgrounDRb</a>使えばいいのか。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/06/rubyonrails-execute-query.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyonRailsでメールを送信するサンプルをもうちょっと詳しく</title>
		<link>http://tt-house.com/2009/05/rubyonrails-about-actionmailer.html</link>
		<comments>http://tt-house.com/2009/05/rubyonrails-about-actionmailer.html#comments</comments>
		<pubDate>Fri, 29 May 2009 09:47:34 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=99</guid>
		<description><![CDATA[RubyOnRailsで日本語メールを送信するでActionMailerの使い方...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F05%252Frubyonrails-about-actionmailer.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22RubyonRails%E3%81%A7%E3%83%A1%E3%83%BC%E3%83%AB%E3%82%92%E9%80%81%E4%BF%A1%E3%81%99%E3%82%8B%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%82%92%E3%82%82%E3%81%86%E3%81%A1%E3%82%87%E3%81%A3%E3%81%A8%E8%A9%B3%E3%81%97%E3%81%8F%22%20%7D);"></div>
<p><a href="http://www.tt-house.com/2008/11/rubyonrails-send-japanese-mail.html">RubyOnRailsで日本語メールを送信する</a>でActionMailerの使い方を簡単に説明しましたが、これだけだと自分の備忘録にすらなっていないので追記。</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>config.action_mailer.delivery_method = :smtp<br />
config.action_mailer.smtp_settings = {<br />
  :address =&gt; &#8216;SMTPサーバ&#8217;,<br />
  :port =&gt; 25,<br />
  :authentication =&gt; :login,<br />
  :user_name =&gt; &#8216;アカウント&#8217;,<br />
  :password =&gt; &#8216;パスワード&#8217;<br />
}</p></blockquote>
<p>上記をconfig/environment.rbに追記。</p>
<p>送信するときには「<a href="http://www.tt-house.com/2008/11/rubyonrails-send-japanese-mail.html">RubyOnRailsで日本語メールを送信する</a>」の例であれば、コントローラ等から</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>Hoge.deliver_sendmail</p></blockquote>
<p>とすればOK。「Hoge」はクラスですね。「deliver_」はお約束です。アンダースコアの後ろがメソッド名になります。～というメソッドに紐付くメールを送信しなさい、っていうイメージですかね。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/05/rubyonrails-about-actionmailer.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyでHTTP経由でのXMLの受信と解析</title>
		<link>http://tt-house.com/2009/05/rubyhttpxml.html</link>
		<comments>http://tt-house.com/2009/05/rubyhttpxml.html#comments</comments>
		<pubDate>Wed, 27 May 2009 20:18:51 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=98</guid>
		<description><![CDATA[まぁ簡単にいうとRSSリーダーみたいなことをしたいときに使います。相手がRSSじ...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F05%252Frubyhttpxml.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Ruby%E3%81%A7HTTP%E7%B5%8C%E7%94%B1%E3%81%A7%E3%81%AEXML%E3%81%AE%E5%8F%97%E4%BF%A1%E3%81%A8%E8%A7%A3%E6%9E%90%22%20%7D);"></div>
<p>まぁ簡単にいうとRSSリーダーみたいなことをしたいときに使います。相手がRSSじゃなくてもOKです。Web上のコンテンツを読み込みたいときに使います。ただ、今回のサンプルだとgetしかできないので、制限はあります。</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p>require &#8216;open-uri&#8217;</p>
<p>open(&#8220;http://www.tt-house.com/atom.xml&#8221;){|f|<br /><http: ?){|f|<br #{params[:id]} parse mecab tools.alpharise.jp / />&nbsp; data = f.read<br />&nbsp; xmldoc = REXML::Document.new data<br />&nbsp; xmldoc.elements.each(&#8220;feed/entry/title&#8221;){|element|<br />&nbsp;&nbsp;&nbsp; p element.text<br />&nbsp; }<br />}</p>
</blockquote>
<p dir="ltr">文字コードのことは気にしてません。</p>
<p dir="ltr">これでハセテツラボRSSのタイトル一覧が取得できます。他のサービスとのマッシュアップ等にも使えますが、やっぱりデータのpostもできないと使い勝手は悪いですよね。</p>
<p dir="ltr">postをするサンプルは次回にしましょう。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/05/rubyhttpxml.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
