LoginSignup
2
2

More than 3 years have passed since last update.

rails データの保存の書き方 (書籍 vs 実務 )

Last updated at Posted at 2020-05-15

書籍などは

def create
  @post = Post.new(post_params)
  if @post.save
    flash[:success] = "保存に成功"
    redirect_to posts_path
  else
    flash[:error] = "保存に失敗"
    redirect_to new_post_path
  end
end

実務では、調査の為の例外のログの吐き出しと、slcakなどの通知ツールする所までがセットで、保存機能が完結する

def create
  @post = Post.new(post_params)
  @post.save! # 例外を発生させる
    flash[:success] = "保存に成功"
    redirect_to posts_path

  rescue StandardError => e
    logger.fatal "#{e.class}: #{e.message}"
    slack_notify(e) # slackへの通知
    flash[:error] = "保存に失敗"
    redirect_to new_post_path
  end
end
2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2