1. 運用 Rails Composer自動建立項目
rails new myapp -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
Rails Composer是Rails App Composer的簡化版本,適合新手使用,教程在這兒https://github.com/RailsApps/rails-composer/
2. 選擇1)
I want to build my own application
因為1)選項可以自行設定所有配置,下面是我選擇的
question Install an example application? 1) 1) I want to build my own application 2) membership/subscription/saas (Stripe or Recurly) 3) rails-prelaunch-signup 4) rails3-bootstrap-devise-cancan 5) rails3-devise-rspec-cucumber 6) rails3-mongoid-devise 7) rails3-mongoid-omniauth 8) rails3-subdomainsquestion Web server for development? 1) 1) WEBrick (default) 2) Thin 3) Unicorn 4) Pumaquestion Web server for production? 3) 1) Same as development 2) Thin 3) Unicorn 4) Pumaquestion Database used in development? 3)注意若選擇非SQLite,需要開啟相應資料庫server 1) SQLite 2) PostgreSQL 3) MySQL 4) MongoDBquestion Template engine? 1) 1) ERB 2) Haml 3) Slim (experimental)question Unit testing? 2) 1) Test::Unit 2) RSpecquestion Integration testing? 2) 1) None 2) RSpec with Capybara 3) Cucumber with Capybara 4) Turnip with Capybaraquestion Fixture replacement? 2) 1) None 2) Factory Girl 3) Machinistquestion Front-end framework? 2)選Zurb貌似會遇到一些問題,這裡選Bootstrap 1) None 2) Twitter Bootstrap 3) Zurb Foundation 4) Skeleton 5) Just normalize CSS for consistent stylingquestion Twitter Bootstrap version? 2) 1) Twitter Bootstrap (Less) 2) Twitter Bootstrap (Sass)question Add support for sending email? 3)這裡是發郵件服務,如果使用Gmail發郵件則選Gmail,我用的是網易企業郵箱,所以選擇SMTP,需要後續配置 1) None 2) Gmail 3) SMTP 4) SendGrid 5) Mandrillquestion Authentication? 2)先用devise,後續可自行添加OmniAuth 1) None 2) Devise 3) OmniAuthquestion Devise modules? 3)Confirmable是需要郵件認證的,本地測試需配置郵件發送,當然也可以手動修改資料表或controller 1) Devise with default modules 2) Devise with Confirmable module 3) Devise with Confirmable and Invitable modulesquestion Authorization? 2) 1) None 2) CanCan with Rolifyquestion Use a form builder gem? 2) 1) None 2) SimpleFormquestion Install a starter app? 4) 1) None 2) Home Page 3) Home Page, User Accounts 4) Home Page, User Accounts, Admin Dashboardextras Add 'therubyracer' JavaScript runtime (for Linux users without node.js)? (y/n) extras Set a robots.txt file to ban spiders? (y/n) nextras Create a project-specific rvm gemset and .rvmrc? (y/n) nextras Create a GitHub repository? (y/n) n
這裡先不建立Github,以後手動添加BitBucket,Gitlab均可
3. 基本程式建立完成
上傳到Github或其他
4. 看一下目錄結構,預設第一個User是 First User,郵箱密碼在application.yml中有,建立新的User我們這裡因為選擇了Confirmable,需要發郵件認證
5. 配置一下郵件發送,我這裡用的是網易企業郵箱
Application.yml的 ADMIN_EMAIL和ADMIN_PASSWORD改成使用郵箱服務的帳號密碼
# 用admin_email發送確認郵件 devise.rb development.rb production.rbADMIN_NAME: iwissenADMIN_EMAIL: admin@iwissen.comADMIN_PASSWORD: passwd
devise.rb的config.mailer_sender改成admin_email
config.mailer_sender = ENV['ADMIN_EMAIL']
development.rb 和 production.rb 添加action_mailer的smtp_settings
config.action_mailer.smtp_settings = { :address => 'smtp.ym.163.com', :port => 25, :domain => 'iwissen.com', :user_name => ENV['ADMIN_EMAIL'], :password => ENV['ADMIN_PASSWORD'], :authentication => :plain }
注意development.rb 的 config.action_mailer.perform_deliveries = true, config.action_mailer.raise_delivery_errors = true 而 production.rb 的 errors設成false
當然,使用SettingLogic gem的可以用Setting.xxx,也可以直接上字串
這裡附上Gmail的設定
config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "smtp.gmail.com", :port => 587, :user_name => 'poshboytl', :password => ENV['GMAIL_PASS'], :authentication => 'plain', :enable_starttls_auto => true }
遇到問題都可以在官方action mailer 中找到http://guides.rubyonrails.org/action_mailer_basics.html
這樣設定就完成了
6. 為了方便和符合國人習慣,把密碼長度設為大於等於6(devise預設8),在devise.rb 中,修改
config.password_length = 6..128
7. rails s 開啟伺服器,註冊新使用者測試,查看console裡的log
Sent mail to dragonszy@gmail.com (671ms)Date: Sun, 26 May 2013 16:28:03 +0800From: admin@iwissen.comReply-To: admin@iwissen.comTo: dragonszy@gmail.comMessage-ID: <51a1c793ba301_d983ffdb6345064853e1@MBP.local.mail>Subject: Confirmation instructionsMime-Version: 1.0Content-Type: text/html; charset=UTF-8Content-Transfer-Encoding: 7bit<p>Welcome dragonszy@gmail.com!</p><p>You can confirm your account email through the link below:</p><p><a href="http://localhost:3000/users/confirmation?confirmation_token=2suzwouTz3NoqbzRKsdU">Confirm my account</a></p>
一般郵件正常發出或者根本沒連的話,瀏覽器不會raise_error,因為設定了smtp串連,所以錯誤只能是連不上的錯誤,檢查使用者密碼是否設定正確,郵箱網域名稱是不是對的
8. 一切搞定後Github push一下
9. 自己建立幾個一般使用者和Admin使用者實驗一下,可見用Rails Composer還是省下了不少功夫的