プロジェクト

全般

プロフィール

操作

ScaffoldPlusAnswerThree » 履歴 » リビジョン 2

« 前 | リビジョン 2/17 (差分) | 次 »
Yuumi Yoshida, 2008-01-13 16:04


= 演習3解答例 =

== 作業手順 ==
{{{
ruby script/generate model user name:string user_id:integer
ruby script/generate migration AddUserIdToTodo user_id:integer
rake db:migrate
}}}

== 変更点 ==

=== 1. app/models/user.rb ===
Todoとの一対多の関連を記述
{{{
#!html
class User < ActiveRecord::Base
has_many :todos
end

}}}

=== 2. app/models/todo.rb ===
Userとの多対一の関連(従属)を記述
{{{
#!html
class Todo < ActiveRecord::Base
belongs_to :user
end

}}}

=== 3. app/views/todos/index.html.erb ===
user.nameカラム表示用のコードを追加
{{{
Listing todos

Due
Name
Task

<%25 for todo in @todos %25>

<%25=h todo.due %25>
<%25=h todo.user.name %25>
<%25=h todo.task %25>
<%25= link_to 'Show', todo %25>
<%25= link_to 'Edit', edit_todo_path(todo) %25>
<%25= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %25>

<%25 end %25>

<%25= link_to 'New todo', new_todo_path %25>
}}}

=== 4. app/views/todos/show.html.erb ===
user.nameカラム表示用のコードを追加
{{{

Due:
<%25=h @todo.due %25>

Task:
<%25=h @todo.task %25>

Name:
<%25=h @todo.user.name %25>

Memo:
<%25=new_line(h(@todo.memo)) %25>

<%25= link_to 'Edit', edit_todo_path(@todo) %25> |
<%25= link_to 'Back', todos_path %25>
}}}

=== 5. app/views/todos/edit.html.erb ===
user.name入力表示用のコードを追加
{{{
Editing todo

<%25= error_messages_for :todo %25>

<%25 form_for(@todo) do |f| %25>

Due
<%25= f.date_select :due %25>

Task
<%25= f.text_field :task %25>

Name
<%25= f.select :user_id, User.find(:all).collect {|u| [ u.name, u.id ] } %25>

Memo
<%25= f.text_area :memo %25>

<%25= f.submit "Update" %25>

<%25 end %25>

<%25= link_to 'Show', @todo %25> |
<%25= link_to 'Back', todos_path %25>
}}}

=== 6. app/views/todos/new.html.erb ===
user.name入力表示用のコードを追加
{{{
New todo

<%25= error_messages_for :todo %25>

<%25 form_for(@todo) do |f| %25>

Due
<%25= f.date_select :due %25>

Task
<%25= f.text_field :task %25>

Name
<%25= f.select :user_id, User.find(:all).collect {|u| [ u.name, u.id ] } %25>

Memo
<%25= f.text_area :memo %25>

<%25= f.submit "Create" %25>

<%25 end %25>

<%25= link_to 'Back', todos_path %25>
}}}

Yuumi Yoshida さんが16年以上前に更新 · 2件の履歴