2007年10月19日金曜日

Viewとにらめっこ

今日はViewについてさらに深く勉強して、
DBの関連についてもやっと「なるほど!」と分かった。
contorollerをさわり、AとBとCのデータベースにそれぞれに登録されている
学生名とか学校名とか出身地をViewにならべて表示出来た!

さて、週明けにこれを仕上げるぞ〜〜

早いもので週明けからはたった4日でOJTが終わります。
ん〜本当に早い!



Listing students





<% for column in Student.content_columns %>

<% end %>

-----------------------------------
 カラム名が入る
-----------------------------------
 ■Studentとは

 [student.rb]

 class Student < ActiveRecord::Base
 belongs_to :school
 has_one :profile
 end

 ■.content_columns()とは
 "_id" または "_count" で終わる全カラム、
 つまりプライマリ id と、単一テーブル継承に
 使用されるカラムを全て取り除いた
 カラムオブジェクトの配列を返します。
 
 ■obj.human_name
  英単語の頭を大文字に変換する。
  日本語の場合不要。
-----------------------------------

<% for student in @students %>
-----------------------------------
 ■@studentsとは

 [ students_controller.rb ]

 def list
  @student_pages, @students =
paginate :students, :per_page => 10
 end
 
 ▽paginate collection_id, options={}

「ページ管理オブジェクト」,「オプションで指定した個数のインスタンスが入ってる」=
paginate「第一引数 ARモデル(モデルクラスの複数形/対象とするテーブル名)」,「オプション」
-----------------------------------

<% for column in Student.content_columns %>


-----------------------------------
 DBの中に入っている値が出る。
-----------------------------------
 ■send(name[, args ... ])
  オブジェクトのメソッド name を、引数に args を渡して呼び出し、
  メソッドの実行結果を返します。
  ブロック付きで呼ばれたときはブロックもそのまま引き渡します。
  メソッド名 name は文字列かSymbol です。
 
 ■obj.name
  オブジェクトのnameを返す。
-----------------------------------
<% end %>





<% end %>
<%= column.human_name %>
<%=h student.send(column.name) %> <%= link_to 'Show', :action => 'show', :id => student %> <%= link_to 'Edit', :action => 'edit', :id => student %> <%= link_to 'Destroy', { :action => 'destroy', :id => student },
:confirm => 'Are you sure?', :method => :post %>



■ update(id, attributes)

update は、複数のカラムを更新し、save します。

update は find(id) して得られる AR オブジェクトに対して update_attributes(attributes) を行います。 update_attributes(attributes) は self.attributes = attributes して save します。戻り値は save の戻り値です。

0 件のコメント: