📘
Favorite.newの省略形
忘れやすいので、メモ✍️
以下2つのコードは同じ意味になる。
favorites_controller.erb
def create
book = Book.find(params[:book_id])
favorite = current_user.favorites.new(book_id: book.id)
favorite.save
redirect_to books_path
end
def create
book = Book.find(params[:book_id])
favorite = Favorite.new
favorite.user_id = current_user.id
favorite.book_id = book.id
favorite.save
redirect_to books_path
end
Discussion
commentsバージョンはこちら