Skip to content

Instantly share code, notes, and snippets.

@JamesYang76
JamesYang76 / coding_tips.md
Last active February 26, 2021 15:22
rails_basic

Coding tips

refer to https://legacy.gitbook.com/book/spartchou/ruby-on-rails-basic/details

Save model

Do not use @post.user_id = current_user.id, and use save! instead of save unless check return value of save

class PostsController < ApplicationController
# @post.user_id = current_user.id and 
  def create
    @post = current_user.posts.build(params[:post])
    @post.save!