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
*/
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
--
----------------------------
--
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
--
----------------------------
--
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)。修改数据库配置文件如下图:![](https://p-blog.csdn.net/images/p_blog_csdn_net/kunshan_shenbin/R1.bmp)
目录结构:(可以使用IDE的Generators生成control及model组件)![](https://p-blog.csdn.net/images/p_blog_csdn_net/kunshan_shenbin/R2.JPG)
代码:
blog_controller.rb
class
BlogController
<
ApplicationController
def
index
list
render :action
=>
'
list
'
end
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#
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 }
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
def
list
#
@post_pages, @posts = paginate :posts, :per_page => 10
@post
=
Post.find(:all)
end
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
def
show
@post
=
Post.find(params[:id])
end
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
def
new
@post
=
Post.new
end
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
def
edit
@post
=
Post.find(params[:id])
end
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<%
=
render :partial
=>
"
post
"
, :collection
=>
@post.reverse
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<%
=
link_to
'
New post', :action => 'new' %>
new.rhtml
<
h1
>
New post
</
h1
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<%=
form_tag :action
=>
'
create
'
%>
<%=
render :partial
=>
'
form
'
%>
<%=
submit_tag
"
Create
"
%>
<%=
form_tag
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<%=
link_to
'
Back
'
, :action
=>
'
list
'
%>
edit.rhtml
<
h1
>
Editing post
</
h1
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<%=
form_tag :action
=>
'
update
'
, :id
=>
@post
%>
<%=
render :
partial
=>
'
form
'
%>
<%=
submit_tag
'
Edit
'
%>
<%=
form_tag
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<%=
link_to
'
Show
'
, :action
=>
'
show
'
, :id
=>
@post
%>
|
<%=
link_to
'
Back
'
, :action
=>
'
list
'
%>
show.rhtml
<%=
render :partial
=>
"
post
"
, :object
=>
@post
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<%=
link_to
'
Edit
'
, :action
=>
'
edit
'
, :id
=>
@post
%>
|
<%=
link_to
'
Back
'
, :action
=>
'
list
'
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
h2
>
Comments
</
h2
>
<%
for
comment
in
@post.comments
%>
<%=
comment.body
%>
<
hr
/>
<%
end
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<%=
form_tag :action
=>
"
comment
"
, :id
=>
@post
%>
<%=
text_area
"
comment
"
,
"
body
"
%></
br
>
<%=
submit_tag
"
Comment!
"
%>
</
form
>
_form.rhtml
<%=
error_messages_for
'
post
'
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
!
--
[form:post]
-->
<
p
><
label
for
=
"
post_title
"
>
Title
</
label
><
br
/>
<%=
text_field
'
post
'
,
'
title
'
%></
p
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
p
><
label
for
=
"
post_created_at
"
>
Created at
</
label
><
br
/>
<%=
datetime_select
'
post
'
,
'
created_at
'
%></
p
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
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服务器,即可访问。