Base CSS:Formsで定義されているようなタグをActionViewのヘルパーを使って吐き出す。
以下の続き
Radioボタン
app/helpers/application_helper.rbに以下を加えた。
# 縦に並べる場合。 def tb_radio_button_tag(name, value, checked_flg, label_content, inline_flg=nil) if inline_flg.nil? class_str = "radio" else class_str ="radio inline" end label_tag(name, radio_button_tag(name, value, checked_flg)+label_content, :class =>class_str ) end # 横に並べる場合(inline) def tb_radio_button_tag_inline(name, value, checked_flg, label_content) tb_radio_button_tag(name, value, checked_flg, label_content, "inline") end
使い方
<%= tb_radio_button_tag('age', '15', true, '15歳') -%> <%= tb_radio_button_tag('age', '20', false, '20歳') -%> <%= tb_radio_button_tag_inline('age', '30', false, '30歳') -%>
以下のタグが吐き出される
(実際は改行されない) <label class="radio" for="age"> <input checked="checked" id="age" name="age" type="radio" value="15" /> 15歳 </label> (実際は改行されない) <label class="radio" for="age"> <input id="age" name="age" type="radio" value="20" /> 20歳 </label> (実際は改行されない) <label class="radio inline" for="age"> <input id="age" name="age" type="radio" value="30" /> 30歳 </label>
Checkboxの場合
app/helpers/application_helper.rbに以下を加えた。
def tb_check_box_tag(name, value, checked_flg, label_content, inline_flg=nil) if inline_flg.nil? class_str = "checkbox" else class_str ="checkbox inline" end label_tag(name, check_box_tag(name, value, checked_flg)+label_content, :class =>class_str ) end def tb_check_box_tag_inline(name, value, checked_flg, label_content) tb_check_box_tag(name, value, checked_flg, label_content, "inline") end
使い方は、Radioボタンの方と同じなので省略。