Your posts match “ rails ” tag:

這兩星期的Rails筆記

這裡記錄了我被xdite大神電過的東西,希望寫下來可以提醒自己不要再犯同樣的錯誤

  1. 千萬不要在 view 裡面幹出運算或邏輯,把要算的東西用 helper 包起來 render
  2. 要接連續字串,使用 "#{str1} #{str2}" 的方式,不要用 "+"
  3. 寫 rake task 時用到的 model 物件, 必須是 app/models/ 裡面的檔名. 而不是 db 實際的 table 名. (當然按照慣例, 這兩者會是一樣的. 但如有不同時, 以 app/models/ 裡的檔名為準.
  4. 在寫 customized 404 / 500 page 時, routes.rb 的 unless 判斷要放在 code 的最下面. 才不會把所有 request 都截下來變 404.
  5. 在 Rails Console 裡要試驗 module 的方法 include ModuleName
  6. object 狀態要在 model 裡 def method 來定義,而不是由 controller 從 object 的 column 去組出狀態 ( 直觀 <=> 非直觀 )
  7. 命名原則要讓人看的懂. ex auth_gsm => 到底是表達「認證的狀態」還是「認證用的手機號碼」,詳細可以參照這篇
  8. HTTP method 動詞要慎用, 見附表.
  9. 開發版的假圖要丟在 public/fake/ 而不是 assets/images/, 避免在正式版本會一起壓進 assets 然後又忘記刪除.
  10. To Be Continued
methods
Action   HTTP Method  Purpose
-------------------------------------------------------------------------
index    GET          Displays a collection of resources
show     GET          Displays a single resource
new      GET          Displays a form for creating a new resource
create   POST         Creates a new resource (new submits to this)
edit     GET          Displays a form for editing an existing resource
update   PUT          Updates an existing resource (edit submits to this)
destroy  DELETE       Destroys a single resource

jQuery-Fileupload-Rails 0.4.1 配 Rails 3.2.13 的坑

第一坑是 view 的 form_for 裡面的 f.file_field 自己多加了 [].

<%= f.file_field(:image, :multiple => true, :name => "product_photos[image]") %>
上面那段會產生
<input id="photo_image" multiple="multiple" name="product_photos[image][]" type="file">
要改成
<input id="photo_image" multiple="multiple" name="product_photos[image]" type="file">

第二坑是 view 部份的 js 要更動

var fu = $('#fileupload').data('fileupload'), 

to

var fu = $('#fileupload').data('blueimpFileupload'),

第三坑是 controller 的 format.json 要變成

format.json {
  render :json => [@photo.to_jq_upload].to_json
}

to

format.json {
  render json: {files: [@photo.to_jq_upload]},
         status: :created,
         location: new_admin_product_photo_path(@product)
}

文件都不講的, 有夠坑 -_-