广告

本站里的文章大部分经过自行整理与测试

2016年7月26日星期二

Ruby on Rails - CRUD

1.安装 Ruby + Rails + Nodejs
 

# Ubuntu / Linux Mint / Debian
$ su
$ apt-get install ruby-full nodejs sqlite3

# Fedora
$ su

$ dnf install ruby nodejs sqlite-devel

# CentOS

$ su
$ yum install ruby sqlite-devel
# 安装 nodejs
http://jasonmun.blogspot.my/2016/07/centos-nodejs.html

# Mageia

$ su
$ urpmi ruby nodejs sqlite3-tools 

# Manjaro
$ pacman -S ruby nodejs sqlite


# OpenSUSE
$ su

$ zypper install ruby nodejs sqlite3

$ ruby -v
$ node --version 或 nodejs --version
$ sqlite3 --version

$ gem install rails
$ rails --version


2.产生 blog project
$ rails new blog

$ cd blog
$ bundle install
$ bundle exec spring binstub --all


3.启动WEB服务器
$ bin/rails server


http://localhost:3000


4.产生 welcome 页面
$ bin/rails generate controller welcome index

create app/controllers/welcome_controller.rb
route  get 'welcome/index'
invoke erb
create app/views/welcome
create app/views/welcome/index.html.erb
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
invoke assets
invoke coffee     
create app/assets/javascripts/welcome.coffee
invoke scss     
create app/assets/stylesheets/welcome.scss

http://localhost:3000/welcome/index


5.设置 config/routes.rb
Rails.application.routes.draw do
  resources :articles
  root 'welcome#index'
end


articles_path     
GET    /articles(.:format)           articles#index
POST   /articles(.:format)          articles#create
 

new_article_path
GET    /articles/new(.:format)      articles#new
 

edit_article_path 
GET    /articles/:id/edit(.:format)  articles#edit
 

article_path
GET    /articles/:id(.:format)        articles#show
PATCH  /articles/:id(.:format)      articles#update
PUT    /articles/:id(.:format)        articles#update
DELETE /articles/:id(.:format)    articles#destroy

6.产生 articles controller
$ bin/rails g controller articles

create  app/controllers/articles_controller.rb
invoke  erb
create  app/views/articles
invoke  test_unit
create  test/controllers/articles_controller_test.rb
invoke  helper
create  app/helpers/articles_helper.rb
invoke  test_unit
invoke  assets
invoke  coffee
create  app/assets/javascripts/articles.coffee
invoke  scss
create  app/assets/stylesheets/articles.scss


7.准备 articles 输入界面
# app/views/articles/new.html.erb
<%= form_for :article, url: articles_path do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>


8.编写 controller
 
# http://localhost:3000/articles/new
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  def new

        # 以上 url new.html.erb
  end
  def create

        # 按 submit 后..
      render plain: params[:article].inspect
  end
end


9.产生 Article model
$ bin/rails generate model Article title:string text:text

invoke active_record
create db/migrate/20160726041332_create_articles.rb
create app/models/article.rb
invoke test_unit
create test/models/article_test.rb
create test/fixtures/articles.yml

 

10.db移植
$ bin/rake db:migrate

11.存资料
# app/controllers/articles_controller.rb
 
def create
  @article = Article.new(article_params)

  @article.save
  redirect_to @article
end

 
private
  def article_params
    params.require(:article).permit(:title, :text)
  end

 

12.准备 http://localhost:3000/articles/1

# app/controllers/articles_controller.rb
def show
  @article = Article.find(params[:id])
end

# app/views/articles/show.html.erb
<p><strong>Title:</strong><%= @article.title %></p>
<p><strong>Text:</strong><%= @article.text %></p>


13.准备 http://localhost:3000/articles

# app/controllers/articles_controller.rb
def index
  @articles = Article.all
end

# app/views/articles/index.html.erb
<h1>Listing articles</h1>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
    </tr>
  <% end %>
</table>


14.添加链接

# app/views/welcome/index.html.erb
<%= link_to 'My Blog', controller: 'articles' %>
<%= link_to 'New article', new_article_path %>

# app/views/articles/new.html.erb
<%= link_to 'Back', articles_path %>

# app/views/articles/show.html.erb
<%= link_to 'Back', articles_path %>

15.添加数据验证

# app/models/article.rb
class Article < ActiveRecord::Base
  validates :title, presence: true,
                    length: { minimum: 5 }
end

# app/controllers/articles_controller.rb
def new
  @article = Article.new
end

def create
  @article = Article.new(article_params)

  if @article.save
    redirect_to @article
  else
    render 'new'
  end
end

private
  def article_params
    params.require(:article).permit(:title, :text)
  end

# http://localhost:3000/articles/new
# app/views/articles/new.html.erb

<%= form_for :article, url: articles_path do |f| %>

  <% if @article.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
    <ul>
    <% @article.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
  <% end %>

  <p><%= f.label :title %><br><%= f.text_field :title %></p>
  <p><%= f.label :text %><br><%= f.text_area :text %></p>
  <p><%= f.submit %></p>
<% end %>

<%= link_to 'Back', articles_path %>

16.
准备 http://localhost:3000/articles/1/edit

# app/controllers/articles_controller.rb
def edit
  @article = Article.find(params[:id])
end

# app/views/articles/edit.html.erb
<h1>Editing article</h1>
<%= form_for :article, url: article_path(@article), method: :patch do |f| %>

  <% if @article.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
    <ul>
    <% @article.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
  <% end %>
 
  <p><%= f.label :title %><br><%= f.text_field :title %></p>
  <p><%= f.label :text %><br><%= f.text_area :text %></p>
  <p><%= f.submit %></p>
<% end %>

<%= link_to 'Back', articles_path %>

# app/controllers/articles_controller.rb
def update
  @article = Article.find(params[:id])

  if @article.update(article_params)
    redirect_to @article
  else
    render 'edit'
  end
end

private
  def article_params
    params.require(:article).permit(:title, :text)
  end
 
# app/views/articles/index.html.erb
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th>
<%= link_to '[New article]', new_article_path %></th>
  </tr>

<% @articles.each do |article| %>
  <tr>
    <td><%= article.title %></td>
    <td><%= article.text %></td>
    <td><%= link_to '[Show]', article_path(article) %> | <%= link_to '[Edit]', edit_article_path(article) %></td>
  </tr>
<% end %>
</table>
<%= link_to 'New article', new_article_path %>

# app/views/articles/show.html.erb
<%= link_to 'Back', articles_path %> | <%= link_to 'Edit', edit_article_path(@article) %>

17.使用局部视图去掉视图中的重复代码

# app/views/articles/_form.html.erb
<%= form_for @article do |f| %>

  <% if @article.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
    <ul>
    <% @article.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
  <% end %>
 
  <p><%= f.label :title %><br><%= f.text_field :title %></p>
  <p><%= f.label :text %><br><%= f.text_area :text %></p>
  <p><%= f.submit %></p>
<% end %>

# app/views/articles/new.html.erb
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>

# app/views/articles/edit.html.erb
<h1>Edit article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>

18.删除 article

# app/controllers/articles_controller.rb
def destroy
  @article = Article.find(params[:id])
  @article.destroy

  redirect_to articles_path
end

# 放入删除连接
# app/views/articles/index.html.erb
<h1>Listing Articles</h1>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th><%= link_to '[New article]', new_article_path %></th>
  </tr>

<% @articles.each do |article| %>
  <tr>
    <td><%= article.title %></td>
    <td><%= article.text %></td>
    <td><%= link_to '[Show]', article_path(article) %> |
    <%= link_to '[Edit]', edit_article_path(article) %> |
    <%= link_to '[Destroy]', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
  </tr>
<% end %>
</table>

没有评论:

发表评论