AWDwR "Chapter17 Action View" つづきのつづき
やべっ、また半年ほどサボるところだったよ:-)。
Form Helpers
送信されてきたフォームのパラメタは以下のようにparamsハッシュにマップされる。
id=123 #=> {:id => "123"} user[name]=Dave #=> {:user => {:name => "Dave"}} user[address][city]=Wien #=> {:user => {:address => {:city => "Wien"}}}
テンプレート内のフォームはform_tag()とend_form_tag()で囲むこと。フォームのヘルパメソッドを以下で順に説明するよ。
まずはテキスト系のフィールド。普通のテキスト、ヒドゥンテキスト、パスワード用テキストがある。
text_field(:variable, :attribute, options) hidden_field(:variable, :attribute, options) password_field(:variable, :attribute, options)
オプションは:size=>"nn", :maxsize=>"nn"。
次にテキストエリア。
text_area(:variable, :attribute, options)
オプションは:cols=>"nn", rows=>"nn"。
ラジオボタンは以下のようにする。
radio_button(:variable, :attribute, tag_value, options)
チェックボックスは以下のようにする。
check_box(:variable, :attribute, options, on_value, off_value)
選択リストはこんな感じ。
select(:variable, :attribute, choices, options, html_options) 例 <%= select(:user, :name, %w{Andy Bert Chas Dave}) %> <%= select(:user, :id, [['Andy', 1], ['Bert', 2], ['Chas', 3], ['Dave', 4]]) %> <%= collection_select(:user, :name, @users, :id, :name) %>
グループ化された選択リスト?っつーのもある。
<select name="order[shipping_option]" id="order_shipping_option"> <%= option_groups_from_collection_for_select(SHIPPING_OPTIONS, :options, :type_name, :id, ;name. @order.shipping_option) %> </select>
日付や時刻は専用のヘルパメソッドがある。
date_select(:variable, :attribute, options) datetime_select(:variable, :attribute, options) select_date(date, options) select_day(date, options) select_month(date, options) select_year(date, options) select_datetime(date, options) select_hour(time, options) select_minute(time, options) select_second(time, options) select_time(time, options)
例えばselect_minuteはparams[:date][:minute]で値を取得できる。また:include_blank=>trueとすると空の選択肢を設定できる。
select_minuteでは:minute_step=>nnというオプションで分の刻み幅を設定。
select_monthでは:add_month_numbers=>trueで月の番号も表示、use_month_numbers=>trueでは月の番号のみ表示。
select_yearはデフォルトでは現在の前後5年を選択肢として表示するけど、開始年、終了年を指定したい場合は:start_year, :end_yearで設定する。
date_select, datetime_selectは年月日、年月日時分秒のメニューを表示する。discard_month=>1等とすればそのフィールドは表示されなくなり、特に1つだけ指定した場合はそれ以降のフィールドは表示されなくなる。また:order=>[:day, :month, :year]とすればその順でフィールドを表示する。
で、ファイルのアップロード。なんだかややこしいのぉ。ちょっと端折るよ。まずは以下のようにアップロードする画像ファイルを選択するフォームを表示する。
<%= form_tag({:action => 'save'}, :multipart => true) %> Upload file: <%= file_field("picture", "picture") %> <%= submit_tag("Upload") %> <%= end_form_tag %>
モデルはちょっと一工夫。
class Picture < ActiveRecord::Base def picture=(picture_field) self.name = base_part_of(picture_field.orginal_filename) self.content_type = picture_field.content_type.chomp self.data = picture_field.read end def base_part_of(file_name) ... end end
アップロードされた画像ファイルを保存するアクションはこうして…
def save @picture = Picture.new(params[:picture]) if @picture.save redirect_to(:action => 'show', :id => @picture.id) else render(:action => :get) end end
画像データを送信するアクションはこうする。
def picture @picture = Picture.find(params[:id]) send_data(@picture.data, :filename => @picture.name, :type => @picture.content_type, :disposition => "inline") %>
で、最後に画像を表示するためのテンプレート。
<img src="<%= url_for(:action => "picture", :id = @picture.id) %>
ところでヘルパメソッドは対象のモデルのバリデーションでエラーだった場合は、生成するHTMLをclass="fieldWithErrors"を属性として持ったdivタグで囲むので、適当にスタイルシートを定義しておくとエラー時にビックリさせることができる。またerror_message_on(), error_message_for()を使えばエラーメッセージが表示できる。
それから、今までのヘルパメソッドはモデルに対応したものだったけどモデルに対応しないヘルパメソッドもあって、それらはメソッド名の最後に_tagをつけまーす。
とゆーことで、まだまだChapter17はつづく…。
ライブドア資本論
- 作者: 佐々木俊尚
- 出版社/メーカー: 日本評論社
- 発売日: 2005/06
- メディア: 単行本
- クリック: 13回
- この商品を含むブログ (24件) を見る
新刊かと思ったら6月に出ていたのね。何軒か本屋を回ってようやく入手。こんな本が出ていたなんて知らなかったからあまり話題になっていなかったのかもしれないけど、ひさびさのヒット。面白い。
連日大騒ぎにはなっていたけど中身はよくわかんなかったライブドアvsフジテレビ騒動がドキュメントで描かれ、そこに登場した金融のテクニックが詳しく説明される。フジテレビを買収するために社債で調達した800億円、ライブドアは一銭も返済する必要はないんですってよ! じゃあこの800億円は結局誰が負担したことになるかというと…もうビックリですよ、奥さん。フーッ、ため息出ちゃいますよ。頭イイ人が勝つんですなぁ。
他にも、団塊ジュニアとしての堀江社長の思考や行動を分析し、それを理解できない旧世代を小気味よく切り捨て、一方、バブル崩壊後の不況からネットバブル、金融ビッグバンを経て現在に至る日本経済の変化とそれにうまく乗った堀江社長とライブドアについて詳しく描かれる。
ちょっと時機を逸した感じだけどもオススメ。