Rails Study(II)Create Sample Post module

本文详细介绍了使用Rails框架创建示例帖子模块的过程,包括生成控制器、视图文件、路由配置、资源创建、迁移运行、链接添加、模型验证、应用控制台测试等步骤。还展示了如何通过Scaffolding快速生成资源模型,并实现数据库操作、布局定制、新建、显示、编辑和删除帖子的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rails Study(II)Create Sample Post module

4. Hello Rails
>rails generate controller home index
Find the file /app/views/home/index.html.erb and add one line <h1>Hello, Rails!</h1>

>rm public/index.html

Open the file config/routes.rb in your editor.
Railsexample::Application.routes.draw do
root :to => "home#index"

Visit the page, http://localhost:3000

5. Getting Up and Running Quickly with Scaffolding
6. Creating a Resource
>rails generate scaffold Post name:string title:string content:text
scaffold can create the model, controller, migrate db and pages.

6.1 Running a Mirgration
>rake db:migrate
This script will create the table in config/database.yml on development database.
The table has columns id, name, title, content, created_at, updated_at.

6.2 Adding a Link
<%= link_to "My Blog", posts_path %> on home page.
The link_to method is one of Rails’ built-in view helpers. It creates a hyperlink based on text to display and where to go –
in this case, to the path for posts.

That is great, I can test these file in my local server.

6.4. The Model
class Post < ActiveRecord::Base
end
Active Record supplies a great deal of functionality to your Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations

6.5 Adding Some Validation
class Post < ActionRecord::Base
validates :name, :presence =>true
validates :title, :presence =>true,
:length => { :minimum => 5}
end

6.6 Using the Console
That is really a nice place to test my applications.
>rails console
>>p=Post.new(:content => "new post for fun")
>>p.save
>>p.errors
=> #<ActiveModel::Errors:0x2983410 @base=#<Post id: nil, name: nil, title: nil, content: "test for fu
n", created_at: nil, updated_at: nil>, @messages={:name=>["can't be blank"], :title=>["can't be blank
", "is too short (minimum is 5 characters)"]}>

6.7. Listing All Posts
check the controller.rb classes.
# GET /posts
# GET /posts.json
def index
@posts = Post.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
Post.all calls the Post model to return all of the posts currently in the database
http://localhost:3000/posts for HTML, and will go to index.html.erb
http://localhost:3000/posts.json for JSON response.

6.8 Customizing the Layout
Layout file in /app/views/layouts/application.html.erb
We can make some changes here:
<!DOCTYPE html>
<html>
<head>
<title>Railsexample</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body style="background: #EEEEEE;">

<%= yield %>

</body>
</html>

6.9 Creating New Posts
edit.html.erb and new.html.erb all render to _form.html.erb

6.10 Showing an Individual Post
6.11. Editing Posts
I notice that I can get the xml format page in this way:
http://localhost:3000/posts.xml
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
format.xml { render xml: @posts }
end

6.12 Destroying a Post

references:
http://guides.rubyonrails.org/getting_started.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值