首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Ruby on Rails 環境建置與開發流程完整筆記:從安裝到 Scaffold 實戰

     編輯:Java 相關知識

記錄 Rails 開發環境建置(Windows)與標準開發流程,避免重複踩坑。涵蓋 Ruby/Rails 安裝、DevKit、NetBeans 整合、MySQL 設定、專案建立、Scaffold 快速原型、驗證、關聯、路由設定全過程。

環境安裝步驟

  1. Ruby Installer:rubyinstaller-1.9.2(含 RubyGems)
  2. Rails:gem install rails
  3. DevKit:下載解壓至 Ruby 安裝目錄,編譯原生擴充套件(如 mysql2)必備
  4. NetBeans 6.9.1:設定 Ruby 平台、Gem 路徑,重點安裝 mysql2 0.2.6 支援 MySQL
  5. ruby-debug-ide:除錯器(持續除錯中)

標準開發流程

1. 建立專案

rails new blog

2. 設定 database.yml

development:
  adapter: mysql2
  encoding: utf8
  database: blog_development
  username: root
  password: 1234
  host: localhost

3. 建立資料庫

rake db:create  # 依 database.yml 自動建庫,無需手動

4. 啟動伺服器驗證

rails server
# 瀏覽 https://127.0.0.1:3000 確認環境變數正常

5. 產生 Controller

rails generate controller home index

6. 設定首頁路由

# 刪除 public/index.html
# config/routes.rb
root :to => "home#index"

7. Scaffold 產生 CRUD 原型

rails generate scaffold Post name:string title:string content:text
rake db:migrate  # 同步 Schema 至資料庫

8. 首頁連結至 Scaffold

<!-- app/views/home/index.html.erb -->
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", posts_path %>

9. Model 驗證

class Post < ActiveRecord::Base
  validates :name, :presence => true
  validates :title, :presence => true, :length => { :minimum => 5 }
end

10. 建立關聯 Model

rails generate model Comment commenter:string body:text post:references
# 自動產生 belongs_to :post
rake db:migrate

11. 設定雙向關聯

class Post < ActiveRecord::Base
  has_many :comments
  # 驗證...
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

12. 巢狀路由

resources :posts do
  resources :comments
end

13. Comments Controller

rails generate controller Comments

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment])
    redirect_to post_path(@post)
  end
end

14. View 整合留言表單

<!-- app/views/posts/show.html.erb -->
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :commenter %><br/>
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br/>
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

關鍵指令速查表

任務指令
建立專案rails new app_name
產生 Scaffoldrails g scaffold Model field:type ...
產生 Modelrails g model Model field:type ...
產生 Controllerrails g controller Name action1 action2
執行遷移rake db:migrate
回滾遷移rake db:rollback
建立資料庫rake db:create
啟動伺服器rails s
進入 Consolerails c

常見坑點提醒

  • mysql2 版本:Rails 3.x 需 mysql2 ~> 0.2.x,新版不相容
  • DevKit 未安裝:gem install mysql2 會報編譯錯誤
  • 編碼問題:database.yml 必須設 encoding: utf8
  • 路由優先序:root :to 應置於 routes.rb 早期
  • 巢狀資源:posts_path vs post_comments_path(@post) 區分清楚

總結

Rails 開發流程極度標準化:資料庫設定 → 遷移 → Scaffold → 驗證 → 關聯 → 巢狀路由 → Controller/View 微調。掌握這套流程,即可在數分鐘內產出功能完整的 CRUD 原型,再疊加業務邏輯逐步演進為正式產品。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved