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

質問編集履歴

2

情報の追記

2020/09/01 12:40

投稿

hurousyotoku500
hurousyotoku500

スコア27

title CHANGED
File without changes
body CHANGED
@@ -93,4 +93,81 @@
93
93
  end
94
94
  end
95
95
 
96
+ ```
97
+ scores/index.html.etb
98
+ ```erb
99
+ <h1>スコアカード</h1>
100
+
101
+ <ul>
102
+ <% @scores.each do |score| %>
103
+ <li><%= score.id %> H: Par<%= score.par %> <%= link_to score.hole_score , score %> <%= score.hole_score - score.par %> </li>
104
+
105
+ <% end %>
106
+ </ul>
107
+ <p>今日のスコアは
108
+ <%= @total_score %>でした
109
+ </p>
110
+
111
+ <div class="center jumbotron">
112
+ <div class="text-center">
113
+ <h1>Welcome to the Microposts</h1>
114
+ <%= link_to 'Sign up now!', signup_path, class: 'btn btn-lg btn-primary' %>
115
+ </div>
116
+ </div>
117
+ ```
118
+ scores_controller.rb
119
+ ```ruby
120
+ class ScoresController < ApplicationController
121
+ def index
122
+ @scores = Score.all
123
+ @total_score =Score.all.sum(:hole_score)
124
+
125
+ end
126
+ def show
127
+ @score = Score.find(params[:id])
128
+ end
129
+ def create
130
+ @score = Score.new(score_params)
131
+
132
+ if @score.save
133
+ flash[:success] = 'Score が正常に投稿されました'
134
+ redirect_to @score
135
+ else
136
+ flash.now[:danger] = 'Score が投稿されませんでした'
137
+ render :new
138
+ end
139
+ end
140
+
141
+ def edit
142
+ @score = Score.find(params[:id])
143
+ end
144
+
145
+ def update
146
+ @score = Score.find(params[:id])
147
+
148
+ if @score.update(score_params)
149
+ flash[:success] = 'Score は正常に更新されました'
150
+ redirect_to @score
151
+ else
152
+ flash.now[:danger] = 'Score は更新されませんでした'
153
+ render :edit
154
+ end
155
+ end
156
+
157
+ def destroy
158
+ @score = Score.find(params[:id])
159
+ @score.destroy
160
+
161
+ flash[:success] = 'Score は正常に削除されました'
162
+ redirect_to scores_url
163
+ end
164
+
165
+ private
166
+
167
+ # Strong Parameter
168
+ def score_params
169
+ params.require(:score).permit(:hole_score)
170
+ end
171
+ end
172
+
96
173
  ```

1

情報の追記

2020/09/01 12:40

投稿

hurousyotoku500
hurousyotoku500

スコア27

title CHANGED
File without changes
body CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  現在、簡単なゴルフスコア入力アプリをrailsにて制作中で、ユーザー機能を付け加えているところでした。
4
4
  セオリーどおり、モデルをつくって、コントローラ、ビューを設定して、ブラウザで〇〇/usersにアクセスしましたが、トップページと全く同じ表示になってしました。
5
+ 実現させたいのは、ユーザーインデックスを表示させたいです。
6
+ Userインスタンスはいくつか作ってあります。
5
7
 
6
8
  ルーティングが怪しいかなと思ってみましたが、
7
9
  ```