データベースの仕組みとRubyプログラムの整理用サイトです

学校をサンプルにして、担任教師1人と生徒多数という関係を表現してみます。
とくに、この関係を利用して次の2つの機能を実現します。
  • 生徒からみて担任教師をドロップダウンで選択する。
  • 教師の詳細画面で生徒数を表示する。

コマンド

まずは次のとおりコマンドを実行する。
D:\rails>rails new sample1
D:\rails>cd sample1
D:\rails\sample1>rails g scaffold teacher name:string
D:\rails\sample1>rails g scaffold student name:string teacher:references
D:\rails\sample1>rake db:migrate
D:\rails\sample1>

モデル

データの関連をつけるため、modelファイルを確認します。
D:\rails\sample1\app\models\student.rb
class Student < ActiveRecord::Base
  belongs_to :teacher #この行は自動入力されている
end
D:\rails\sample1\app\models\teacher.rb
class Teacher < ActiveRecord::Base
  has_many :students #この行は手で追加する
end

ビュー

studentsのviewファイルを確認します。

D:\rails\sample1\app\views\students\_form.html.erb
<%= form_for(@student) do |f| %>
  <% if @student.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@student.errors.count, "error") %> prohibited this student from being saved:</h2>
 
      <ul>
      <% @student.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
 
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :teacher %><br />
    <%= f.collection_select(:teacher_id, Teacher.find(:all), :id, :name) %> #ここを変更
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
D:\rails\sample1\app\views\students\edit.html.erb
<h1>Editing student</h1>
 
<%= render 'form' %>
 
<%= link_to 'Show', @student %> |
<%= link_to 'Back', students_path %>
D:\rails\sample1\app\views\students\index.html.erb
<h1>Listing students</h1>
 
<table>
  <tr>
    <th>Name</th>
    <th>Teacher</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
 
<% @students.each do |student| %>
  <tr>
    <td><%= student.name %></td>
    <td><%= student.teacher.name %></td>
    <td><%= link_to 'Show', student %></td>
    <td><%= link_to 'Edit', edit_student_path(student) %></td>
    <td><%= link_to 'Destroy', student, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>
 
<br />
 
<%= link_to 'New Student', new_student_path %>
D:\rails\sample1\app\views\students\new.html.erb
<h1>New student</h1>
 
<%= render 'form' %>
 
<%= link_to 'Back', students_path %>
D:\rails\sample1\app\views\students\show.html.erb
<p id="notice"><%= notice %></p>
 
<p>
  <b>Name:</b>
  <%= @student.name %>
</p>
 
<p>
  <b>Teacher:</b>
  <%= @student.teacher.name %>
</p>
 
<%= link_to 'Edit', edit_student_path(@student) %> |
<%= link_to 'Back', students_path %>

teachersのviewファイルを確認します。

D:\rails\sample1\app\views\teachers\show.html.erb
<p id="notice"><%= notice %></p>
 
<p>
  <b>Name:</b>
  <%= @teacher.name %><BR>
  <b>生徒数:</b>
  <%= @teacher.students.size %>

</p>
 
<%= link_to 'Edit', edit_teacher_path(@teacher) %> |
<%= link_to 'Back', teachers_path %>

コマンド

D:\rails\sample1>rails s

このページへのコメント

Qc9Gdq <a href="http://lfhmhxwaenuh.com/">lfhmhxwaenuh</a>, [url=http://euzkajbpszel.com/]euzkajbpszel[/url], [link=http://rkopocypvqob.com/]rkopocypvqob[/link], http://jsdrwuismgvi.com/

0
Posted by dppvtmkrss 2013年05月10日(金) 21:27:58 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

管理人/副管理人のみ編集できます