目次
●概要
●サンプルのポイント
●サンプルの使い方
●ファイル構成
●ビュー(画面)
●コントローラー
内容
概要
チェックボックスを扱うアプリケーションです。
入力画面(index.html.erb)

↓
結果画面(index.html.erb)

サンプルのポイント
・チェックボックスのデフォルト状態(チェックON/OFF)の操作
サンプルの使い方
サンプルはあらかじめ用意していないので、以降の項目を参照にして、自分で作成しなければいけません。作成後に、ブラウザからhttp://127.0.0.1:3000/check_boxにアクセスしましょう(ポート番号"3000"は適宜、環境に応じて読み替えて下さい)。
ファイル構成
本項で作成するファイル
ファイル名 | 種類 |
index.html.erb | ビュー |
check_box_controller.rb | コントローラー |
ビュー(画面)
入力画面、兼、結果画面を作成しましょう。
"ror-tutorial\app\views\check_box\index.html.erb"ファイルを以下の内容で作成します。
<html>
<head>
<title>Tutorial: Checkbox index</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<%= stylesheet_link_tag 'ror' %>
</head>
<body>
<h2>Tutorial: Checkbox index</h2>
<% form_tag :action => 'submit' do %>
<%= check_box_tag 'check1', "1", @check1 %>check1
<%= check_box_tag 'check2', "1", @check2 %>check2<br />
<%= submit_tag "サブミット" %>
<% end %>
</body>
</html>
コントローラー
"ror-tutorial\app\controllers\check_box_controller.rb"ファイルを以下の内容で作成します。
class CheckBoxController < ApplicationController
#入力画面表示
def index
@check2 = true
render :action => 'index.html.erb'
end
#結果画面表示
def submit
@check1 = false
@check2 = false
render :action => 'index.html.erb'
end
end