貼code

2017年12月2日 星期六

Corsair K70 RGB 心得&mac驅動

前一陣子美國Amazon黑五購物節,趁機會敗了一把K70 RGB 紅軸官方修復版
不過要注意的是,官方修復版沒有附拔鍵器,保固上也只有90天 。
但考量到相同價錢  買全新的只有單色紅軸,一直想玩燈的我
還是決定賭一把買了RGB版本的(非LUX)
加運費一共是92.08塊美金。選的是普通運輸,上星期六(11.25)早上訂購昨天早上就收到了
約一個禮拜。比起的預訂的12/08快很多。這點很滿意。話不多說先上圖~
箱子&本體

心得部分,將會與手上FILCO忍紅做個比較

  1. 外觀:不得不說K70的質感真的很棒,髮絲紋背板+多媒體按鍵選鈕真是視覺與觸覺的兩大享受。質感方面與FILCO真是不相上下,都會讓人忍不住多摸幾下XDD。不過相較於忍紅走的簡潔路線,Corsair的燈光與LOGO就感覺比較浮誇一些,不過兩種不同風格實在難以比較。享受的樂趣也就有點不同拉~
  2. 手感:雖然兩者都是紅軸,但打起來的感覺還是略有不同。我的忍紅已換上金鍵盤購入的PBT鍵帽,K70則是全原廠。K70打起來稍微軟(感覺鬆鬆的)一些,觸底的感覺與忍紅不太一樣,可能是衛星軸的關係,K70的長鍵打感真的偏肉。整體來說比較沒有忍紅那個麽直上直下。打感方面個人是覺得忍紅略勝。不過K70的ABS鍵帽觸感很棒,希望不要像FILCO一樣快打油QQ
  3. 燈光:這方面K70是我第一把RGB鍵盤,透過驅動的調整我是選則Rainbow + Type lighting。雖然打字的時候不會一直看著鍵盤,但餘光喵到以及不打字思考時看著閃亮亮的鍵盤總覺得心情不錯。K70的亮度即使最亮也不會閃到讓人不舒服。也可以透過快捷鍵調整,睡覺時可直接關閉燈光相當方便。
  4. 總結:這次會買K70得原因,除了Amazon特價又送台灣,也是想換一把100%的鍵盤長駐在家中使用。K70 RGB讓我相當滿意,加上又有個很大很舒服的手托。真的是很適合接上桌電使用。看來忍紅接下來會是去公司或學校的好夥伴,而K70就會在桌上成為主力鍵盤拉!

接下來講一下如何在Mac 上使用K70 RGB吧。目前官方並沒有提供驅動,不過網路上已經有開源的驅動可以下載。
我的系統是OS X High Sierra。但我安裝的時候pkg檔尚未開放,所以只能自己載code下來編。
需要先安裝Xcode與QT5,按照README中的只是應該就可以成功安裝,我是按照Build.md中的先裝完Xcode&Qt後。把repo的ZIP檔下載,解壓縮& cd 進資料夾後,在終端機執行./qmake-auto && make  就成功拉。
資料夾中會出現一個CKB的.app檔,執行看到以下畫面就可以開始調整摟!!
(如果沒有成功可以重新開機看看)
CKB.app執行結果
感謝各位收看~

2017年11月16日 星期四

安裝Node.js on OSX sierra

步驟:
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

2017年8月15日 星期二

rails administrate 排序

administrate是一個gem,用來作為rails app的後台系統。他有許多優點像介面清楚,擴充及客製化程度頗高等等。這邊要來記錄如何設定administrate的各個model在點選的時候的default order。
裝好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

capistrano deploy remote not found

解法:ssh-agent
Generating a new SSH key and adding it to the ssh-agent

2017年7月31日 星期一

Rails 的 DB export 成 excel檔

最近在弄的案子需要一個功能,就是能一鍵將後台的資料匯出整理成xlsx檔
原本看到一個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。要怎麼辦呢?

提供以下兩種做法:

  1. 直接在belongs_to: xxx 後面加上 optional: true
  2. 在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

2017年2月12日 星期日

將Carrierwave與Administrate 整合

    Carrierwave 是 Rails 裡常用的照片上傳的一個gem , 而Administrate是一個較新推出的admin管理gem 。今天要來記錄如何將Carrierwave當成一個Administrate的custom field

(關於Carrierwave的設定可參考 https://medium.com/@mauddev/rails-5-and-carrierwave-53960ec20c4b#.d9sdv7r1w)

再新增完一個uploader(假設叫ImageUplader),並在想加入照片的model(假設是Post)裡加入一個欄位(假設叫 :img, :string)之後,在administrate的post_dashboard裡
ATTRIBUTE_TYPE中加入
image: PostImageField
PostImageField可以自己換成其他名字,然後在其他地方加入image的這個欄位
再來終端機上輸入
rails generate administrate:field post_image
然後就會發現在controller/field 裡出現一個post_image_controller
在裡面新增一個method
  def to_s
    data.url
  end
接著,會在view/fields中看到三個partial ,_form代表在admin頁面中的編輯頁面如何顯示
所以將它改成可以吃一個檔案
<div class="field-unit__label">
  <%= f.label field.attribute %>
</div>
<div class="field-unit__field">
  <%= f.file_field field.attribute %>
</div>

然後_index 跟 _show裡加上
 
<%= image_tag field.to_s %>

這樣就行摟~
下次再更新如何加入thumb 跟 small的照片部分吧!!

參考資料:
https://administrate-prototype.herokuapp.com/adding_custom_field_types

2017年2月6日 星期一

Rails 購物車 cart的model部份

Rails 購物車cart 的部分。
有幾點值得注意:
  1. 功能非常基本 , 有update的部分需要修改
  2. 購物車的基本概念是,使用者在購物時會新增一個session,這個session是一個hash,命名為@product,裡頭會以各個product的id作為key,以product的amount作為value。
  3. 當確定要結帳時,再將cart送到訂單,訂單部分再接Active Record再連到資料庫。如此一來可以避免產生許多未完成的購物資料(沒真正要買)。 
code 部分:
class Cart
  def initialize(session_cart)
    if session_cart
      @products = session_cart.map{|key, val| [key.to_i, val]}.to_h
    else
      @products = Hash.new
    end
  end

  def update(product_id, amount)
    if amount == 0
      @products[product_id] = nil
    else
      @products[product_id] = amount
    end
  end

  def add(product_id, amount = 1)
    if @products[product_id]
      @products[product_id] += amount
    else
      @products[product_id] = amount
    end
  end

  def to_h
    @products
  end

  def count
    @products.count
  end

  def each
    return enum_for(:each) unless block_given?

    @products.each do |product_id, amount|
      product = Product.find_by_id(product_id)
      if product
        yield product, amount
      end
    end
  end

  def total
    @products.reduce(0) do |total, (product_id, amount)|
      product = Product.find_by_id(product_id)
      if product
        total + product.price * amount
      else
        total
      end
    end
  end
end

解釋:

  1. initialize的部分,要先確定瀏覽器裡是否已經有session_cart了,如果有,則將session_cart 裡的每個key變成interger。但map完會變成一個array,所以用hash內建的method把array變回hash再塞回@product
  2. update部分 清空一個key value的部份需修改。否則的話,@product就會以product_id為key ,amount為value來修改這對key value。
  3. add概念類似第二點
  4.  to_h為cart這個類別新稱一個method,使一個cart類別下的物件可以用.to_h得到@product這個hash變數
  5.  count目的為了算出有幾件商品,.count為hash變數的方法,算出有幾個key value pair
  6. total目的是為了算出總共多少錢,reduce(0)代表total從0開始。傳入@product裡的product_id 及 amount。設一個product變數去連接到資料庫去確定有沒有id 為 product_id的這件商品,有的話total就加上amount*product.price,沒有的話就維持

rails devise 使用者編輯資料時免當前密碼

    在開發rails 網站時,常常會用到devise這個gem來負責處理使用者的部分。
而devise預設的edit功能是需要輸入當前密碼的。有些情況下不希望如此,就可以利用以下方法(客製化devise 的controller)

首先 在 config/routes.rb 裡,加入
devise_for :users, controllers: { registrations: 'users/registrations' }
(如果devise:controller 的scope 是 users的話,即rails g devise:controllers users )

再來在users/registrations_controller.rb裡,加上
protected
def update_resource(resource, params)
    params.delete :current_password
    resource.update_without_password(params)
end

最後把edit.html.erb頁面裡的current password欄位拿掉就可以摟!!

===2017.2.12更新
後來發現這樣做的話,密碼就不能改了OAO
所以把registration_controller裡改一下
變成current_password的欄位還是show出來
然後判斷,如果current欄位跟new_password都是空的
那就不用current_password 就可以改其他資料
protected
  
def update_resource(resource, params)
  if params[:current_password].blank? && params[:password].blank?
    resource.update_without_password(params.except(:current_password))
  else
    super
  end
end


參考資料
https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

2017年2月5日 星期日

隨手記上線摟!

最近開始練習一些Ruby on Rails 的專案,遇到了不少的問題。但隨著出現及解決的問題越來越多,很多時候都會忘了當初解決問題的方法。於是乎想來設個網誌,紀錄一下遇到的問題以及後來解決的方法。所以...請多多指教拉