步驟:
1. brew install nvm
2. 在bash_profile 加入
source $(brew --prefix nvm)/nvm.sh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
(可以解決checksum not match 的問題)
3. .bash_profile 重新reload
4. nvm install v8.9.1
貼code
2017年11月16日 星期四
2017年8月15日 星期二
rails administrate 排序
administrate是一個gem,用來作為rails app的後台系統。他有許多優點像介面清楚,擴充及客製化程度頗高等等。這邊要來記錄如何設定administrate的各個model在點選的時候的default order。
裝好gem之後,在admin/controllers中找到對應該model的.rb檔
加入
當然如果所有的model都要以此規則排序也是可以選擇直接寫在admin/application_controller.rb中
參考資料:https://github.com/thoughtbot/administrate/issues/442
裝好gem之後,在admin/controllers中找到對應該model的.rb檔
加入
before_action :default_params def default_params params[:order] ||= "(欄位名稱)" params[:direction] ||= "(asc/desc)" end把欄位名稱跟遞增or遞減加上之後就完成摟
當然如果所有的model都要以此規則排序也是可以選擇直接寫在admin/application_controller.rb中
參考資料:https://github.com/thoughtbot/administrate/issues/442
2017年7月31日 星期一
Rails 的 DB export 成 excel檔
最近在弄的案子需要一個功能,就是能一鍵將後台的資料匯出整理成xlsx檔
原本看到一個gem叫rails_db,裡面的功能真是包山包海,也有一鍵輸出,
但沒有辦法做一些調整,只能原封不動地將資料丟出來。只好放棄尋找其他的gem。
最終的解法便是axlsx_rails這個gem(https://github.com/straydogstudio/axlsx_rails)
使用方法:
先在Gemfile中加入
接著我是創一個download的controller
然後新增一個method叫order(就是想要dump的資料表的名稱)
最後加上template,記得檔名要寫對 order.xlsx.axlsx,然後會dump出一個orders.xlsx
template的部分就可以整理成想要的格式,最後在route中設定好就可以拉!
如果有接administrate,就可以把download_controller繼承於admin::application_controller這樣他也會吃到authenticate_user 就不怕使用者亂戳不小心就dump下來整個db拉XD
原本看到一個gem叫rails_db,裡面的功能真是包山包海,也有一鍵輸出,
但沒有辦法做一些調整,只能原封不動地將資料丟出來。只好放棄尋找其他的gem。
最終的解法便是axlsx_rails這個gem(https://github.com/straydogstudio/axlsx_rails)
使用方法:
先在Gemfile中加入
gem 'rubyzip', '~> 1.1.0' gem 'axlsx', '2.1.0.pre' gem 'axlsx_rails'然後bundle install
接著我是創一個download的controller
然後新增一個method叫order(就是想要dump的資料表的名稱)
def order @orders = Order.all render xlsx: 'order', template: "download/order.xlsx.axlsx", filename: "orders.xlsx" end
最後加上template,記得檔名要寫對 order.xlsx.axlsx,然後會dump出一個orders.xlsx
wb = xlsx_package.workbook
wb.add_worksheet(name: "Orders") do |sheet|
sheet.add_row ["官網訂單編號", "訂購人會員編號","訂購人", "處理進度"]
@orders.each do |order|
sheet.add_row [order.id, order.user_id, order.p_last_name+order.p_first_name, '']
end
end
template的部分就可以整理成想要的格式,最後在route中設定好就可以拉!
如果有接administrate,就可以把download_controller繼承於admin::application_controller這樣他也會吃到authenticate_user 就不怕使用者亂戳不小心就dump下來整個db拉XD
2017年3月14日 星期二
Devise 的ssl 問題
因為以往的案子很少會用到ssl 驗證。這次剛好又機會要串ssl卻發現facebook登入後回來時會自動從https跳到http。進而造成Authenticity token的error(當使用者下次自https使用網站時)。這問題困擾我好久XD,最後發現是devise的問題,故在此紀錄一下。
我的方法是,先在Nginx或Apache設定redirect 301,強制使用這瀏覽http時自動導倒https。(雖然聽說rails 也可以做到類似事情,不過還是建議在Apache設定。
接著設定devise的部分,如果是用Nginx的話,在nginx.conf中加入
proxy_set_header X-FORWAEDED-PROTO $scheme;
如果是用Apache的話,加入
RequestHeader set X-FORWARDED-PROTO 'https'
參考資料:https://github.com/plataformatec/devise/wiki/How-To:-Use-SSL-(HTTPS)
2017年2月21日 星期二
用production mode 來部署網站
一般情況下,直接在專案底下輸入rails server會使用development mode來啟動rails app。但在真正要上線,必須改成使用production mode。這樣一來,你的rails app跑起來才不會出現紅色的error頁面,再來,rails 在production mode會先將一些css js等放在assets裡的東西預先編譯(pre-compile)好來增加網站的效能。要改成production mode,請先完成以下事項。
首先修改config/secret.yml,設定production mode的secret_key_base。rails 預設已經幫你把develop mode 跟 test mode的給寫好了,但要自己新增production mode中的。原本的secret.yml如下
# Be sure to restart your server when you modify this file. # Your secret key is used for verifying the integrity of signed cookies. # If you change this key, all old signed cookies will become invalid! # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. # You can use `rails secret` to generate a secure secret key. # Make sure the secrets in this file are kept private # if you're sharing your code publicly. development: secret_key_base: aed39a79230a55e79be1e261947e976f66c9830a5b9f438e7b570b3ebbca7b71cbdebbfb7eaef86a06f9a35fc6b3ed257bae12af510dcd23914da0ee32afd157 test: secret_key_base: 589c27fe53513bcbcb0a9ec8e6d050fb0661e073e08b8d6220ccbb4eb305e0b73602f6e5fe8b93f08f2fc49a26baf468c1a22dde3a27f6b87f8b761a0fa3dc7e # Do not keep production secrets in the repository, # instead read values from the environment. production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
在終端機底下輸入
rake secret把生出來的一串東西,取代production: 底下的<%= ENV["SECRET_KEY_BASE"] %>
存擋後,再來在production mode地下的db需要建好。請先把database.yml設定好,看production mode想用哪種sql(通常建議mysql 或 postgresql)
接著在終端機輸入
RAILS_ENV=production bundle exec rake db:migrate
接著要先讓rails 把assets編好,所以一樣在終端機輸入
RAILS_ENV=production bundle exec rake assets:precompile
最後雖然compile了,但production模式預設是不會讓使用者讀取/public/assets的檔案。所以要在config/environments/production.rb裡加上
config.public_file_server.enabled = true
請注意:rails 4以前的版本可能會能夠將config.serve_static_assets = false這行改成true,但在rails 5已經不支援這樣寫了。另外,在網路上有查到
config.assets.serve_static_files = true 這樣的寫法,根據官方文件,這樣寫在rails 5的新一版會被踢除,請改成上面那樣的寫法。
照這樣設定完,就可以在終端機輸入以下來將rails app以production mode跑起來摟!
rails s -e production
參考資料:http://easonchang.logdown.com/posts/2016/08/30/start-your-rails-project-with-production-mode
2017年2月15日 星期三
Rails 5的外部鍵問題
Rails 5中有個更新,就是以往外部鍵的驗證要自己加,也就是說外部鍵可以是nil。但在Rails 5中,這個設定被取消了。也就是說,所有的外部鍵都必須要存在。而最近的案子中好死不死就需要讓一個order可以是非會員所下的。也就是order的user_id要為nil。要怎麼辦呢?
提供以下兩種做法:
提供以下兩種做法:
- 直接在belongs_to: xxx 後面加上 optional: true
- 在controller裡加入(套用在整個application)
Rails.application.config.active_record.belongs_to_required_by_default = false
參考資料:http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html
訂閱:
意見 (Atom)