teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

endが抜けていた部分を修正

2020/11/17 08:39

投稿

necocoa
necocoa

スコア209

title CHANGED
File without changes
body CHANGED
@@ -82,6 +82,7 @@
82
82
  redirect_to posts_path, flash: { error: @post.flash_error_hash }
83
83
  else
84
84
  render :edit
85
+ end
85
86
  end
86
87
 
87
88
  private

1

refererを読むタイミングをリダイレクト前に変更した

2020/11/17 08:39

投稿

necocoa
necocoa

スコア209

title CHANGED
File without changes
body CHANGED
@@ -29,7 +29,7 @@
29
29
  1.画面の遷移先をrefererで確認して条件分岐しているが、もっと良いやり方がないか?
30
30
  ```ruby
31
31
  referer_uri = URI.parse(request.referer)
32
- if referer_uri.path != posts_path
32
+ if referer_uri.path == posts_path
33
33
  ...
34
34
  end
35
35
  ```
@@ -72,19 +72,16 @@
72
72
  @post = Post.find(params[:id])
73
73
  referer_uri = URI.parse(request.referer)
74
74
 
75
- if referer_uri.path != posts_path
76
- if @post.update(post_params)
75
+ if @post.update(post_params)
76
+ if referer_uri.path == posts_path
77
- redirect_to @post, notice: 'Post was successfully updated.' and return
77
+ redirect_to posts_path, notice: 'Post was successfully updated.'
78
78
  else
79
- render :edit and return
79
+ redirect_to @post, notice: 'Post was successfully updated.'
80
80
  end
81
- end
82
-
83
- if @post.update(post_params)
81
+ elsif referer_uri.path == posts_path
84
- redirect_to posts_path, notice: 'Post was successfully updated.'
82
+ redirect_to posts_path, flash: { error: @post.flash_error_hash }
85
83
  else
86
- redirect_to posts_path, flash: { error: @post.flash_error_hash }
87
- end
84
+ render :edit
88
85
  end
89
86
 
90
87
  private