9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

redirect_toにand returnを組み合わせると、リダイレクトする前にアクション内の処理を全て実行できる

Posted at

redirect_to :root and returnのand returnの部分に注目して下さい。
redirect_to :rootを書くだけではアクション内の処理は終了せず次の行のコードを実行していってしまいます。
今回のケースでは下に続く処理がないので問題ないのですが、慣習的につけるようにするのがベターです。

def create Question.create(create_params) redirect_to :root and return end

private def create_params params.require(:question).permit(:text).merge(user_id: current_user.id, group_id: current_user.group_id) end

9
4
0

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
9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?