LoginSignup
1
1

More than 3 years have passed since last update.

redirect_backで直前のページにバック! ❏Rails❏

Last updated at Posted at 2019-12-03

現在ツイート詳細ページ(show.html.haml)でコメントができます。
alt

ツイートにコメントをしたらコメントする前の状態に戻るようにします。
redirect_back(fallback_location: アクション名)で直前のページに戻すことができます。

app/controllers/comments_controller.rb
def create
  tweet = Tweet.find(params[:tweet_id])
  @comment = tweet.comments.build(comment_params)
  if @comment.save
    redirect_back(fallback_location: tweet_path(tweet))
  else
    flash[:danger] = "テキストを入力してください"
    redirect_back(fallback_location: tweet_path(tweet))
  end
end

tweet_pathはshowアクションのprefixです。

以上!



参考

https://railsguides.jp/layouts_and_rendering.html



ではまた!

1
1
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
1
1