KulsWiki
- apt-get install rails
- apt-get install mysql-server
- sudo su
- /etc/init.d/mysql start
- (mysql 내부에서 create database [프로젝트명]_development, utf-8 설정)
- (예: create database naltle_development default character set utf8; )
- cd /var/www
- rails [프로젝트명]
- cd [프로젝트명]
- vi config/database.yml
- 각 섹션(development, test, production) 밑에서 공백 두 칸 띄고, encoding:utf8 추가. 만일 root 비밀번호 따로 설정되어 있다면 password: 옆에 입력.
- ./script/server
- 파이어폭스에서, http://localhost:3000으로 서버가 제대로 작동하는지 페이지 확인
- ./script/generate model post
- vi db/migrate/001_create_post.rb
- 3번째 줄 밑에
- t.column 'title', :string
- t.column 'body', :text
- t.column 'author', :string
- t.column 'created_at', :datetime 추가
- rake db:migrate (마이그레이트는 그냥 시험적으로 해본 것입니다.)
- ./script/console (irb가 필요할 수 있습니다.)
- a = Post.new
- a.title = 'title'
- a.body = 'blah blah'
- a.author = 'kulsian'
- a.datetime =
- ./script/generate scaffold post post
- 앞의 post는 model, 뒤의 post는 controller
- http://localhost:3000/post 파이어폭스로 접속.
- vi app/views/post/list.rhtml
<h1>Listing posts 글 목록</h1>
<table>
<tr>
<!--<% for column in Post.content_columns %>
<th><%= column.human_name %></th>
<% end %>-->
<th>번호</th>
<th>제목</th>
<th>작자</th>
<th>시간</th>
</tr>
<% for post in @posts %>
<tr>
<td><%= post.id %></td>
<td><%= link_to post.title, :action=>'show', :id=> post %></td>
<td><%= post.body %></td>
<td><%= post.created_at %></td>
<!--<% for column in Post.content_columns %>
<td><%=h post.send(column.name) %></td>
<% end %>
<td><%= link_to 'Show', :action => 'show', :id => post %></td>
<td><%= link_to 'Edit', :action => 'edit', :id => post %></td>
<td><%= link_to 'Destroy', { :action => 'destroy', :id => post }, :confirm => 'Are you sure?', :method => :post %></td>
--></tr>
<% end %>
</table>
<%= link_to 'Previous page', { :page => @post_pages.current.previous } if @post_pages.current.previous %>
<%= link_to 'Next page', { :page => @post_pages.current.next } if @post_pages.current.next %>
<br />
<%= link_to 'New post', :action => 'new' %>
- vi app/views/show.rhtml
<% for column in Post.content_columns %>
<p>
<b><%= column.human_name %>:</b> <%=h @post.send(column.name) %>
</p>
<% end %>
<%= link_to 'Edit', :action => 'edit', :id => @post %> |
<%= link_to 'Back', :action => 'list' %> |
<% for comment in @post.comments %>
<p>
<b>작성자: <%=comment.author%>,내용: <%= comment.body%>: 시간: <%= comment.created_at %></b>
</p>
<p>
<%= link_to 'Del', :action =>'delete', :id=>comment %>
</p>
<% end %>
<% form_for :comment, @comment, :url => { :controller => :comment, :action =>'create' } do |f| %>
<%= f.hidden_field :post_id, :value => @post.id %>
<%= f.text_field :author %>
<%= f.text_field :body %>
<%= submit_tag 'replysubmit' %>
<% end %>
- ./script/generate controller post
- vi app/controllers/post_controller.rb
class PostController < ApplicationController
def index
list
render :action => 'list'
end
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def list
@post_pages, @posts = paginate :posts, :per_page => 2, :order => "created_at DESC"
end
def show
@post = Post.find(params[:id])
@comment = Comment.new
end
def new
@post = Post.new
end
def create
@post = Post.new(params[:post])
if @post.save
flash[:notice] = 'Post was successfully created.'
redirect_to :action => 'list'
else
render :action => 'new'
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
flash[:notice] = 'Post was successfully updated.'
redirect_to :action => 'show', :id => @post
else
render :action => 'edit'
end
end
def delete
Comment.find(params[:id]).destroy
redirect_to :back
end
def destroy
Post.find(params[:id]).destroy
redirect_to :action => 'list'
end
end
- ./script/generate controller comment
- vi app/controllers/comment_controller.rb
class CommentController < ApplicationController
def create
@comment = Comment.new(params[:comment])
if @comment.save
flash[:notice] = 'ok'
redirect_to :back
else
redirect_to :back
end
end
end