Essentail Rails Design Pattern

Write Good Rails Code

在 Controller 裡處理資料

場景

#TODO

Wrong

1
2
3
4
5
6
def checkout
  book = Book.find(params[:id])
  current_user.balance -= book.price
  curremt_user.save!
  redirect_to account_path
end

### Correct

1
2
3
4
5
def checkout
  book = Book.find(params[:id])
  current_user.purchase(book)
  redirect_to account_path
end

Comments