요리법을 분류 체계별로 나누기

우리가 만든 요리책은 요리법(recipe)과 분류 체계(categories)를 갖게 되었다. 지금은 두개의 테이블이 연결되어 있지 않기 때문에 요리법에 분류 체계를 지정해 주어야 한다. 이것을 하기 위해서는 recipe 테이블에 category id를 추가해야 한다. 또한 요리법 수정 화면에 분류 체계를 선택할 수 있는 drop-down list를 추가해야 한다.

recipe 테이블에 category 테이블의 id와 맵핑하는 int(11) 타입의 category_id 필드를 추가한다.(역자 주 : 원문에서는 int(6) 이나 이 글에서 사용하는 MySQL-Front에서는 int(11) 타입을 id의 기본형으로 만들어 준다.)


그림 48. category_id 필드가 추가된 recipe 테이블

이제는 recipe과 category model을 수정하자 . 각각 c:\rails\cookbook\app\models\recipe.rb 파일과 c:\rails\cookbook\app\models\category.rb 파일을 열어서 아래의 코드를 추가하자. 그림 49, 50을 참조하자.


그림 49. recipe 모델 수정


그림 50. category 모델 수정

위에 추가한 코드는 Rails에게 recipe 테이블은 category 테이블의 하나의 row에 속해야 하며, category 테이블의 row는 n개의 recipe 테이블 row와 관련된다는 것을 알려 준다.

예를 들어 recipe 모델에 해당하는 @recipe 객체를 만들고, @recipe.category.name으로 recipe이 속한 category의 이름을 참조할 수 있다. 반대로 category 모델에 해당하는 @category 객체를 만들고, @category.recipes으로 category에 속한 recipes 집합을 참조할 수도 있다.

recipe 수정 화면에서 category를 선택하려면, recipe action 중에 하나인 edit 함수와 edit 템플릿을 수정해야 한다. 우선 c:\rails\cookbook\app\controllers\recipe_controller.rb 파일을 열어서 그림 51과 같이 수정하자.


그림 51. recipe controller의 새로운 edit 함수

위의 함수에 추가된 두 줄의 코드는 수정할 recipe을 담고 있는 @recipe 객체와, recipe에 부여할 category를 담고 있는 @categories 집합을 생성한다. 여기서 생성된 객체는 view template에 전달되고, html 렌더링에 사용된다.

edit 함수를 작성했으니, 수정에 사용될 edit template을 만들어 보자. c:\rails\cookbook\app\views\recipe 폴더를 열고 edit.rhtml 파일을 만들어서 아래의 내용을 입력하자.

<html>
<head>
<title>Edit Recipe</title>
</head>
<body>
<h1>Edit Recipe</h1>

<form action=”../update/<%= @recipe.id %>” method=”POST”">
<input id=”recipe_id” name=”recipe[id]” size=”30″
type=”hidden” value=”<%= @recipe.id %>” />
<p><b>Title</b><br>
<input id=”recipe_title” name=”recipe[title]” size=”30″
type=”text” value=”<%= @recipe.title %>” />
</p>
<p><b>Description</b><br>
<input id=”recipe_description” name=”recipe[description]”
size=”30″ type=”text”
value=”<%= @recipe.description %>” />
</p>
<p><b>Category:</b><br>

<select name=”recipe[category_id]”>
<% @categories.each do |category| %>
<option value=”<%= category.id %>”
<%= ‘ selected’ if category.id == @recipe.category_id %>>
<%= category.name %>
</option>
<% end %>
</select></p>

<p><b>Instructions</b><br>
<textarea cols=”40″ id=”recipe_instructions”
name=”recipe[instructions]”
rows=”20″ wrap=”virtual”>
<%= @recipe.instructions %>
</textarea> </p>
<input type=”submit” value=”Update” />
</form>

<a href=”/recipe/show/<%= @recipe.id %>”>
Show
</a> |
<a href=”/recipe/list” mce_href=”/recipe/list” >
Back
</a>

</body>
</html>

edit 함수에서 만든 @recipe, @categories 객체가 사용되는 방법을 알 수 있을 것이다. <select/> 태그 안에서 사용되는 loop를 살펴 보면, option 태그에서 recipe에 지정된 category를 찾는 방법을 알 수 있다.

웹 브라우저에 http://127.0.0.1:3000/recipe/list 입력하고, 분류 항목을 “Ice waters”에서 “Beverages”로 바꾸어 보자.


그림 52. 요리법 항목 바꾸기

다음 단계로 넘어 가기 전에 모든 요리법의 category를 갱신해야 한다. category가 지정되지 않은 요리법이 있으면, 다음 단계에서 에러가 발생하기 때문이다.

요리법 목록에 분류 체계 보여 주기

c:\rails\cookbook\app\views\recipe\list.rhtml 파일을 아래와 같이 수정하자.

<html>
<head>
<title>All Recipes</title>
</head>
<body>

<h1>Online Cookbook - All Recipes</h1>
<table border=”1″>
<tr>
<td width=”40%”><p align=”center”><i><b>Recipe</b></i></td>
<td width=”20%”><p align=”center”><i><b>Category</b></i></td>
<td width=”20%”><p align=”center”><i><b>Date</b></i></td>
</tr>

<% @recipes.each do |recipe| %>
<tr>
<td><%= link_to recipe.title, :action => “show”, :id => recipe.id %></td>
<td><%= recipe.category.name %></td>
<td><%= recipe.date %></td>
</tr>
<% end %>
</table>
<p><%= link_to “Create new recipe”, :action => “new” %></p>

</body>
</html>

웹 브라우저에 http://127.0.0.1:3000/recipe/list를 입력하자. 아래의 그림과 같이 출력되는가?


그림 53. 분류 체계가 표시되는 요리법 리스트

끝.


글 보관함

카운터

Total : / Today : / Yesterday :
get rsstistory!