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
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