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

質問編集履歴

3

ソースコードの変更点を反映しました。

2020/05/13 01:41

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -11,17 +11,16 @@
11
11
  @user = User.new(user_params)
12
12
  if @user.save
13
13
  flash[:notice] = '会員登録完了あなたは#{@user.id}人目のサービス利用者なので貢献度を#{@user.id}ポイントプレゼントします(´・ω・`)'
14
+ redirect_to("/")
14
15
  else
15
- # render :new
16
- flash[:notice] = '会員登録失敗'
16
+ flash[:alert] = "会員登録失敗"
17
+ render action: :new
17
- end
18
+ end
18
- # @user.level += @user.id
19
19
  end
20
20
  ```
21
21
  new.html.erb
22
22
  ```
23
23
  <% @user = User.new unless @user %>
24
- <%= flash[:notice] %>
25
24
  <%= form_for @user,:url => {controller: "home", action: "create" } do |f| %>
26
25
  <p>名前</p>
27
26
  <%= f.text_field :name %>
@@ -39,9 +38,13 @@
39
38
  ```
40
39
  create.html.erb
41
40
  ```
41
+ <p>submitボタンを押すと遷移する画面</p>
42
42
  <% if flash[:notice].present? %>
43
43
  <div class="notice"><%= flash[:notice] %></div>
44
44
  <% end %>
45
+ <% if flash[:alert].present? %>
46
+ <div class="alert"><%= flash[:alert] %></div>
47
+ <% end %>
45
48
  ```
46
49
 
47
50
  ### 補足情報

2

viewファイルの情報追記

2020/05/13 01:41

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -5,21 +5,21 @@
5
5
  会員登録画面でsubmitボタンを押してもフラッシュが表示されない。
6
6
 
7
7
  ### 該当のソースコード
8
-
9
- ```app/controllers/home_controller.rb
8
+ app/controllers/home_controller.rb
9
+ ```
10
10
  def create
11
11
  @user = User.new(user_params)
12
12
  if @user.save
13
13
  flash[:notice] = '会員登録完了あなたは#{@user.id}人目のサービス利用者なので貢献度を#{@user.id}ポイントプレゼントします(´・ω・`)'
14
14
  else
15
- render :new
15
+ # render :new
16
16
  flash[:notice] = '会員登録失敗'
17
17
  end
18
18
  # @user.level += @user.id
19
19
  end
20
20
  ```
21
-
22
- ```new.html.erb
21
+ new.html.erb
22
+ ```
23
23
  <% @user = User.new unless @user %>
24
24
  <%= flash[:notice] %>
25
25
  <%= form_for @user,:url => {controller: "home", action: "create" } do |f| %>
@@ -36,9 +36,9 @@
36
36
  <br>
37
37
  <%= f.submit "新規登録!" %>
38
38
  <% end %>
39
-
40
39
  ```
41
- ```create.html.erb
40
+ create.html.erb
41
+ ```
42
42
  <% if flash[:notice].present? %>
43
43
  <div class="notice"><%= flash[:notice] %></div>
44
44
  <% end %>

1

flashの表示ビューの追加

2020/05/11 08:00

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -10,21 +10,18 @@
10
10
  def create
11
11
  @user = User.new(user_params)
12
12
  if @user.save
13
- flash[:notice] = '会員登録完了あなたは#{@user.id}人目のサービス利用者なので貢献度を#{@user.id}ポイントプレゼントします(´・ω・`)'
13
+ flash[:notice] = '会員登録完了あなたは#{@user.id}人目のサービス利用者なので貢献度を#{@user.id}ポイントプレゼントします(´・ω・`)'
14
- @user.level += @user.id
15
- redirect_to("/")
16
14
  else
17
- flash.now[:alert] = '会員登録失敗'
18
- render :new
15
+ render :new
16
+ flash[:notice] = '会員登録失敗'
19
- end
17
+ end
18
+ # @user.level += @user.id
20
19
  end
21
20
  ```
22
21
 
23
22
  ```new.html.erb
24
23
  <% @user = User.new unless @user %>
25
- <% if @user.errors.any? %>
24
+ <%= flash[:notice] %>
26
- <p>会員登録が失敗しました(´・ω・`)</p>
27
- <% end %>
28
25
  <%= form_for @user,:url => {controller: "home", action: "create" } do |f| %>
29
26
  <p>名前</p>
30
27
  <%= f.text_field :name %>
@@ -39,10 +36,12 @@
39
36
  <br>
40
37
  <%= f.submit "新規登録!" %>
41
38
  <% end %>
39
+
42
40
  ```
43
41
  ```create.html.erb
44
- <p>会員登録のボタンを押した後の画面</p>
42
+ <% if flash[:notice].present? %>
45
-
43
+ <div class="notice"><%= flash[:notice] %></div>
44
+ <% end %>
46
45
  ```
47
46
 
48
47
  ### 補足情報