ラベル form の投稿を表示しています。 すべての投稿を表示
ラベル form の投稿を表示しています。 すべての投稿を表示

2009年8月28日金曜日

Railsの日付選択ヘルパー (select_date)

Rails2.3で日付選択ヘルパー(select_date)を使うためのメモ。

オプションとして指定できるものはこんな感じのようだ。

<%
options = {
 :prefix => 'payday', # field名のprefix
 :order => [:month, :year, :day], # 表示順
 :date_separator => '/', # 項目間の区切り
 :prompt => true, # 選択リストの一番上の表示について。個別の指定も可
 :include_blank => true, # 選択リストの一番上のブランクにする
 :use_month_numbers => true, # 月を数字で表す

 # 選択可能な年の範囲を指定
 :start_year => Date.today.year,
 :end_year => Date.today.year + 1,

 # 非表示にする
 :discard_year => true,
 :discard_month => true,

 :discard_day => true
}
html_options = {} # ?
%>
<%= select_date(Date.today + 2.days, options, html_options) %>


また、Rails2.2にはバグがあるらしい → エラー回避 date_select -- can't convert Symbol into String - 税理士業界でSaaS開発をしながら綴る日記


select_dateとdate_selectは似てるけど別モノらしい。
年月日を個々に作る場合、「select_year, select_month, select_day」が使えるが、「year_select, month_select, day_select」は無い。



参考:
 Module: ActionView::Helpers::DateHelper (本家リファレンス)
 変なヤバいもんログ » Railsで忘れそうなHelperメソッドメモ

 いきなりはまった。select_date - 印刷屋のdeveloper日記

Railsプラグイン ActiveForm調査メモ

AtcitveFormはテーブルに紐付かないモデルを使って入力フォームを作るためのプラグイン。
データの入れ物とvalidationを担う。

「ActiveForm」という名前のプラグインは複数あるので注意。
Gemでインストールすると、module版のActiveFormがインストールされるが、よく分からなかったのでパス。

どうやら「RealityForge」で公開されたActiveFormが主流のようだ。


最新版?:
 maciej's active_form at master - GitHub


使い方:
 RailsのActiveFormの使い方 - 京の路
 Ruby on Rails プラグイン まとめ wiki - active_formプラグイン
 るびすけの開発日記 ~Ruby on Rails~ - IT業界のための転職サイト -



Rails2.2以降ではエラーが発生する。

undefined method `self_and_descendants_from_active_record' for Xxx:Class

rails2.2でactive_formを導入するときにはまった - オレワカ。を参考に、「self_and_descendants_from_active_record」を追加すると解消した。
svnから持ってくるバージョンでは 「self_and_descendents_from_active_record」 すらも無いので注意。


使用例
#コントローラ
class SearchController < ApplicationController
 def index
  @search = Search.new(params[:search])
  if params[:search]
   @search.valid?
  end
 end
end
#モデル
require 'active_form'

class Search < ActiveForm
 attr_accessor :tag
 validates_presence_of :tag
 validates_length_of :tag, :maximum => 3
 def validate
  # カスタム入力チェックはここで
 end
end
<!-- ビュー -->
<% form_for(:search, @search) do |f| %>
<%= error_messages_for :search, "tag" %>
<%= f.text_field "tag" %>
<% end %>

2008年11月14日金曜日

PHPでサーバサイドでデータをPOSTする色々な方法











とても簡単にPHPからPOSTリクエストを送信する方法 [PHP, Tips] - Programming Magicがまとまっていて分かりやすい。

関連記事:floatingdays: PHPを使ってサーバサイドでXMLをPOST


2008/11/28 追記
POSTするときのContent-typeをオプションのheader(http header)として指定しないとNoticeが出る。その場合は"application/x-www-form-urlencoded"が適用される。
また、Content-typeに"text/html"などを指定すると受け側でエラーを起こすことがあるので注意。Railsとか。

2008年11月8日土曜日

CakePHP 1.2のFormHelperのradio()で余分なhiddenを出力しない方法

CakePHP1.2のFormHelperでラジオボタンを出力すると、ラジオボタンの前に隠し項目が出力されることがある。


<input type="hidden" name="data[Foo][bar]" id="FooBar_" value="" />
<input type="radio" name="data[Foo][bar]" id="FooBarBaz" value="Baz" />
<label for="FooBarBaz">Baz</label>

このhiddenを出力させたくない場合、$form->radio()の第3引数の配列で、'value'に何か値をセットして渡す。
echo $form->radio('Foo.bar', array('Baz' => 'Baz'), array('value' => 'none'));
そうすると、
<input type="radio" name="data[Foo][bar]" id="FooBarBaz" value="Baz" />
<label for="FooBarBaz">Baz</label>

ラジオボタンの値に一致するvalueを渡すとそれが初期選択状態になるので、選択状態にしたくない場合は上記の例のように一致しない適当なvalueを渡す。


参考:API for CakePHP : The PHP Rapid Development Framework :: version 1.2.x.x

ブログ アーカイブ

tags