您的位置:首页 > 编程语言 > Ruby

Ruby On Rails的一个简单实例

2008-04-09 10:19 761 查看
IDE: Apatana 主页地址: http://www.aptana.com/

DB: MySQL

安装ROR环境:

Ruby: http://www.ruby-lang.org/ 或者 http://rubyinstaller.rubyforge.org/wiki/wiki.pl 下载安装

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


-- ----------------------------


CREATE TABLE `comments` (


`id` int(11) NOT NULL auto_increment,


`body` text,


`post_id` int(11) default NULL,


PRIMARY KEY (`id`)


) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;




-- ----------------------------


-- Table structure for posts


-- ----------------------------


CREATE TABLE `posts` (


`id` int(11) NOT NULL auto_increment,


`title` varchar(255) default NULL,


`created_at` datetime default NULL,


`body` text,


PRIMARY KEY (`id`)


) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;




-- ----------------------------


-- Records


-- ----------------------------


INSERT INTO `comments` VALUES ('1', 'aaa', '2');


INSERT INTO `comments` VALUES ('2', 'bbb', '2');


INSERT INTO `comments` VALUES ('3', 'ddd', '2');


INSERT INTO `comments` VALUES ('4', '', '1');


INSERT INTO `comments` VALUES ('5', 'aaa', '1');


INSERT INTO `posts` VALUES ('1', 'a', '2008-04-08 18:18:00', 'a');


INSERT INTO `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服务器,即可访问。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: