1. 程式人生 > >Ruby On Rails的一個簡單例項

Ruby On Rails的一個簡單例項

DB: MySQL

安裝ROR環境:

Rails: gem install rails --include-dependencies

新建資料庫blogs,並執行以下SQL文建表。

/*
MySQL Data Transfer
Source Host: localhost
Source Database: blogs
Target Host: localhost
Target Database: blogs
Date: 2008/04/09 9:43:11
*/


SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
--
 Table structure for comments

--
 ----------------------------
CREATETABLE `comments` (
  `id` 
int(11NOTNULL auto_increment,
  `body` 
text,
  `post_id` 
int(11defaultNULL,
  
PRIMARYKEY  (`id`)
) ENGINE
=MyISAM AUTO_INCREMENT=6DEFAULT CHARSET=utf8;

-- ----------------------------
--
 Table structure for posts
--
 ----------------------------

CREATETABLE `posts` (
  `id` 
int(11NOTNULL auto_increment,
  `title` 
varchar(255defaultNULL,
  `created_at` 
datetimedefaultNULL,
  `body` 
text,
  
PRIMARYKEY  (`id`)
) ENGINE
=MyISAM AUTO_INCREMENT=3DEFAULT CHARSET=utf8;

-- ----------------------------
--
 Records 
--
 ----------------------------
INSERTINTO `comments` 
VALUES ('1''aaa''2');
INSERTINTO `comments` VALUES ('2''bbb''2');
INSERTINTO `comments` VALUES ('3''ddd''2');
INSERTINTO `comments` VALUES ('4''''1');
INSERTINTO `comments` VALUES ('5''aaa''1');
INSERTINTO `posts` VALUES ('1''a''2008-04-08 18:18:00''a');
INSERTINTO `posts` VALUES ('2''b''2008-04-08 18:19:00''b');

新建Rails工程(工程名暫定為:rorblog)。修改資料庫配置檔案如下圖:

目錄結構:(可以使用IDE的Generators生成control及model元件)

程式碼:

blog_controller.rb

class BlogController < 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 => 10
   @post = Post.find(:all)
  end

  
def show
    @post 
= Post.find(params[:id])
  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 destroy
    Post.find(params[:id]).destroy
    redirect_to :action 
=>'list'
  end
  
  
def comment
    Post.find(params[:id]).comments.create(params[:comment])
    flash[:notice] 
="Add Your Comments"
    redirect_to(:action 
=>"show", :id => params[:id])
  end
  
end

blog_helper.rb

module BlogHelper
end

comment.rb

class Comment < ActiveRecord::Base
end

post.rb

class Post < ActiveRecord::Base
  
  validates_presence_of :title
  has_many :comments
  
end

list.rhtml

<h1>My Wonderful WebBolg</h1>

<%= render :partial =>"post", :collection => @post.reverse %>

<%= link_to 'New post', :action => 'new' %>

new.rhtml

<h1>New post</h1>

<%= form_tag :action =>'create'%>
  
<%= render :partial =>'form'%>
  
<%= submit_tag "Create"%>
<%= form_tag %>

<%= link_to 'Back', :action =>'list'%>

edit.rhtml

<h1>Editing post</h1>

<%= form_tag :action =>'update', :id => @post %>
  
<%= render :partial=>'form'%>
  
<%= submit_tag 'Edit'%>
<%= form_tag %>

<%= link_to 'Show', :action =>'show', :id => @post %>|
<%= link_to 'Back', :action =>'list'%>

show.rhtml

<%= render :partial =>"post", :object => @post %>

<%= link_to 'Edit', :action =>'edit', :id => @post %>|
<%= link_to 'Back', :action =>'list'%>

<h2>Comments</h2>
<%for comment in @post.comments %>
    
<%= comment.body %>
    
<hr/>
<% end %>

<%= form_tag :action =>"comment", :id => @post %>
    
<%= text_area "comment""body"%></br>
    
<%= submit_tag "Comment!"%>
</form>

_form.rhtml

<%= error_messages_for 'post'%>

<!--[form:post]-->
<p><label for="post_title">Title</label><br/>
<%= text_field 'post''title'%></p>

<p><label for="post_created_at">Created at</label><br/>
<%= datetime_select 'post''created_at'%></p>

<p><label for="post_body">Body</label><br/>
<%= text_area 'post''body'%></p>
<!--[eoform:post]-->

_post.rhtml

<div>
    
<h2><%= link_to post.title, :action =>'show', :id => post %></h2>
    
<p><%= post.body %></p>
    
<p><small>
       
<%= post.created_at.to_s(:long) %>
       (
<%= link_to 'Edit', :action =>'edit', :id => post %>)
    
</small></p>
  
</div>

啟動Webrick伺服器,即可訪問。