肖威洞察 | Ruby on Rails 的快速學習的體系框架;
Ruby on Rails 的快速學習的體系框架;
參考案例體系:
https://ruby-china.github.io/rails-guides/getting_started.html
https://www.techotopia.com/index.php/Ruby_Essentials
Ruby on Rails 程式設計體系的技能體系的學習的關鍵所在在於:
新手階段 10% ;
1、Ruby 的後端結構的體系的學習;
2、Rails 的前端框架的體系的學習;
3、Ruby on Rails 的案例體系的訓練 ;
高階新手階段 59% ;
4、針對於存在的案例給與專案的復現;
勝任者 20% ;精通者階段 10% ;
5、針對於不存在的案例使用皮膚完成專案分解,從而打造自己的專案;
專家階段 1% ;
6、針對於別人的專案的問題,快速的給與解決的方法和答疑
新建一個專欄
rails new blog
cd blog
git init
git add .
git commit -m "commit initial"
構建一個首頁
bin/rails generate controller Welcome index
atom .
rails s
touch app/views/welcome/index.html.erb
<h1>Hello, Rails!</h1>
config/routes.rb
Rails.application.routes.draw do
root 'articles#index'
resources :articles
end
構建文章列表
git checkout -b add_articles
bin/rails generate controller Articles
bin/rails generate model Article title:string text:text
bin/rails db:migrate
rails s
git add .
git commit -m "add articles"
app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
touch app/views/articles/index.html.erb
<h1>Hello, Rails!</h1>
<%= link_to 'My Blog', controller: 'articles' %>
<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
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/show.html.erb
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<h2>Comments</h2>
<%= render @article.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
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 %>
構建評論頁面
git checkout -b add_comments
bin/rails generate model Comment commenter:string body:text article:references
bin/rails db:migrate
bin/rails generate controller Comments
rails s
config/routes.rb
Rails.application.routes.draw do
root 'articles#index'
resources :articles do
resources :comments
end
end
app/models/article.rb
class Article < ApplicationRecord
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5 }
end
app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :article
end
app/controllers/comments_controller.rb
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
app/views/comments/-comment.html.erb
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.article, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
app/views/comments/-form.html.erb
<%= form_for([@article, @article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
上傳程式碼倉庫和雲端專案部署
git remote add origin https://github.com/shenzhoudance/xiaoweiblog.git
git push --all origin
相關文章
- 肖威洞察 | 如何做好區塊鏈行業的教育體系區塊鏈行業
- 肖威洞察 | 商業社會的深度思考
- Ruby on Rails 生成指定版本的 Rails 專案AI
- rails on ruby,ruby on rails 之程式碼塊(二)AI
- 如何提高 Ruby On Rails 的效能?AI
- Windows平臺下快速搭建Ruby on Rails的開發環境WindowsAI開發環境
- [ruby] rails renderAI
- 為什麼說現在是學習Ruby和Rails的最佳時機!AI
- ruby on rails腳手架之初體驗AI
- java 和 Ruby On Rails的對比JavaAI
- 號稱下一代的Ruby on Rails框架——Meteor介紹AI框架
- ruby on rails 小技巧AI
- Ruby on Rails 相關AI
- Ruby On Rails 技術AI
- Rails並不是用Ruby編寫的AI
- 在Ruby on Rails/Naked Objects精神指引下的域驅動開發框架AIObject框架
- Ruby on Rails Ping ++ 支付AI
- do |r| Ruby & Rails endAI
- 什麼是 Ruby on Rails?AI
- 如何提高 Ruby On Rails 效能AI
- 精簡版的Rails框架->Rails::APIAI框架API
- [Rails學習之路]Rails路由配置AI路由
- 肖威洞察 | 2.5 個人如何開發一款跨終端的產品?[Ruby Tuesday 的分享] · 簡單心理技術團隊...
- Ruby on Rails中的MVC架構是如何工作的AIMVC架構
- ruby on rails筆記和理解AI筆記
- ruby on rails 伺服器配置AI伺服器
- Ruby on Rails?有人會用嗎?AI
- .nil? .empty? .blank? .present? in Ruby on RailsAI
- 為 Web 開發提供的 10 個 Ruby on Rails GemsWebAI
- 為Ruby On Rails開發者準備的5款IDEAIIDE
- Ruby on Rails Mountable vs. Full EngineAI
- Ruby on rails專案中 引入BootstrapAIboot
- 使用 Ruby on Rails 開發 Go 介面AIGo
- Ruby社群應該去Rails化了AI
- ruby on rails 學習---終於搞明白多型,大家不明白的話,可以一起交流下AI多型
- 分享一款免費體驗的快速開發框架,可以學習用!框架
- 26本 Ruby/Rails 相關英文圖書的簡評AI
- [轉載] 快速學習-Mybatis框架概述MyBatis框架