Skip to content

Instantly share code, notes, and snippets.

@kaochenlong
Last active July 20, 2017 12:53
Show Gist options
  • Save kaochenlong/14c5b4032859a898b4a0164845e78db0 to your computer and use it in GitHub Desktop.
Save kaochenlong/14c5b4032859a898b4a0164845e78db0 to your computer and use it in GitHub Desktop.

啟動 Rails Server (for C9)

rails server -b $IP -p $PORT

產生 Product 的指令:

rails generate scaffold product name:string photo:string description:text price:decimal is_available:boolean

建立資料表

rails db:migrate

使用 ActiveAdmin

Step 1 編輯 Gemfile

gem 'activeadmin'

# Plus integrations with:
gem 'devise'
gem 'cancan' # or cancancan
gem 'draper'
gem 'pundit'

Step 2 安裝 ActiveAdmin

rails generate active_admin:install

rails generate active_admin:resource Product

Step 3 允許更新欄位

ActiveAdmin.register Product do
  permit_params do
    [:name, :photo, :description, :price, :is_available]
  end
end

Step 4 建立資料

rails db:migrate

rails db:seed

Step 4 使用後台

檔案上傳

Step 1 編輯 Gemfile

gem 'carrierwave'

Step 2 產生上傳器

rails generate uploader Photo

Step 3 編輯 Product

class Product < ActiveRecord::Base
  mount_uploader :photo, PhotoUploader
end

縮圖

Step 1 安裝 gem

gem 'mini_magick'

Step 2 更新套件

$ sudo apt-get update

$ sudo apt-get install imagemagick
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment