레일즈 입문

KulsWiki

Jump to: navigation, 찾기
  1. apt-get install rails
  2. apt-get install mysql-server
  3. sudo su
  4. /etc/init.d/mysql start
  5. (mysql 내부에서 create database [프로젝트명]_development, utf-8 설정)
  6. (예: create database naltle_development default character set utf8; )
  7. cd /var/www
  8. rails [프로젝트명]
  9. cd [프로젝트명]
  10. vi config/database.yml
  11. 각 섹션(development, test, production) 밑에서 공백 두 칸 띄고, encoding:utf8 추가. 만일 root 비밀번호 따로 설정되어 있다면 password: 옆에 입력.
  12. ./script/server
  13. 파이어폭스에서, http://localhost:3000으로 서버가 제대로 작동하는지 페이지 확인
  14. ./script/generate model post
  15. vi db/migrate/001_create_post.rb
  16. 3번째 줄 밑에
    1. t.column 'title', :string
    2. t.column 'body', :text
    3. t.column 'author', :string
    4. t.column 'created_at', :datetime 추가
    5. rake db:migrate (마이그레이트는 그냥 시험적으로 해본 것입니다.)
  17. ./script/console (irb가 필요할 수 있습니다.)
    1. a = Post.new
    2. a.title = 'title'
    3. a.body = 'blah blah'
    4. a.author = 'kulsian'
    5. a.datetime =
  18. ./script/generate scaffold post post
    1. 앞의 post는 model, 뒤의 post는 controller
  19. http://localhost:3000/post 파이어폭스로 접속.
  20. 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' %>
  1. 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 %>
  1. ./script/generate controller post
  2. 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
  1. ./script/generate controller comment
  2. 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