---
# System prepended metadata

title: RL-10 未完- 新增狀態欄 state
tags: [Rails]

---

# RL-10 未完- 新增狀態欄 state



狀態機
安全更改狀態，而非直接進db改值。
event 作到狀態改變。

安裝 aasm gem
 gem install aasm
bundle install




在Model（note）作
```
include AASM
aasm column: 'state' do
	state :draft, initial: true
	state :public, :hidden

	event :publish do
		transitions from: :draft to: :public
	end

	event :hide do
		transitions from: :public to: :hidden
	end

	event :hide do
		transitions from: [:public, to: :hidden], to: :draft
	end
```
從public與hidden改為draft 改叫 recall
	event :recall do
		transitions from: [:public, to: :hidden], to: :draft
	end

撞名
public改published





###### tags: `Rails`