質問編集履歴

3

「View」「Controller」のコードの追記を行いましたので改善点がありましたらご指摘の方お願いします。

2017/11/10 05:34

投稿

Syota
Syota

スコア28

test CHANGED
File without changes
test CHANGED
@@ -1,319 +1,315 @@
1
1
  railsを使い簡単なアプリの開発を進めているのですが、「Form」に入力したデータを反映させるにはどの様な操作を行えば反映させる事が出来ますか??
2
2
 
3
- viewの設定を行い項目名を表示する事は出来たのですが、入力したデータが反映されず空欄になってしまうので入力データを出力する設定の仕方を教えて頂きたいです。```ここに言語を入力
3
+ viewの設定を行い項目名を表示する事は出来たのですが、入力したデータが反映されず空欄になってしまうので入力データを出力する設定の仕方を教えて頂きたいです。
4
+
5
+
6
+
4
-
7
+ 回答よろしくお願いします。
8
+
9
+
10
+
11
+ 「_form.html.erb」
12
+
13
+
14
+
15
+ ```<%= form_with(model: syotum, local: true) do |form| %>
16
+
17
+ <% if syotum.errors.any? %>
18
+
19
+ <div id="error_explanation">
20
+
21
+ <h2><%= pluralize(syotum.errors.count, "error") %> prohibited this syotum from being saved:</h2>
22
+
23
+
24
+
5
- コード
25
+ <ul>
26
+
27
+ <% syotum.errors.full_messages.each do |message| %>
28
+
29
+ <li><%= message %></li>
30
+
31
+ <% end %>
32
+
33
+ </ul>
34
+
35
+ </div>
36
+
37
+ <% end %>
38
+
39
+
40
+
41
+ <div class="field">
42
+
43
+ <%= form.label :from %>
44
+
45
+ <%= form.text_field :from, id: :syotum_from %>
46
+
47
+ </div>
48
+
49
+
50
+
51
+ <div class="field">
52
+
53
+ <%= form.label :year %>
54
+
55
+ <%= form.number_field :year, id: :syotum_year %>
56
+
57
+ </div>
58
+
59
+
60
+
61
+ <div class="field">
62
+
63
+ <%= form.label :school %>
64
+
65
+ <%= form.text_area :school, id: :syotum_school %>
66
+
67
+
68
+
69
+ <div class="field">
70
+
71
+ <%= form.label :seibetU %>
72
+
73
+ <%= form.text_field :sex, id: :syotum_sex %>
74
+
75
+
76
+
77
+ <div class="actions">
78
+
79
+ <%= form.submit %>
80
+
81
+ </div>
82
+
83
+ <% end %>
6
84
 
7
85
  ```
8
86
 
9
87
 
10
88
 
11
- 回答よろしくお願いします。
12
-
13
-
14
-
15
- _form.html.erb」
89
+ index.html.erb」
16
-
17
-
18
-
19
- ```<%= form_with(model: syotum, local: true) do |form| %>
20
-
21
- <% if syotum.errors.any? %>
22
-
23
- <div id="error_explanation">
24
-
25
- <h2><%= pluralize(syotum.errors.count, "error") %> prohibited this syotum from being saved:</h2>
26
-
27
-
28
-
29
- <ul>
30
-
31
- <% syotum.errors.full_messages.each do |message| %>
32
-
33
- <li><%= message %></li>
34
-
35
- <% end %>
36
-
37
- </ul>
38
-
39
- </div>
40
-
41
- <% end %>
42
-
43
-
44
-
45
- <div class="field">
46
-
47
- <%= form.label :from %>
48
-
49
- <%= form.text_field :from, id: :syotum_from %>
50
-
51
- </div>
52
-
53
-
54
-
55
- <div class="field">
56
-
57
- <%= form.label :year %>
58
-
59
- <%= form.number_field :year, id: :syotum_year %>
60
-
61
- </div>
62
-
63
-
64
-
65
- <div class="field">
66
-
67
- <%= form.label :school %>
68
-
69
- <%= form.text_area :school, id: :syotum_school %>
70
-
71
-
72
-
73
- <div class="field">
74
-
75
- <%= form.label :seibetU %>
76
-
77
- <%= form.text_field :sex, id: :syotum_sex %>
78
-
79
-
80
-
81
- <div class="actions">
82
-
83
- <%= form.submit %>
84
-
85
- </div>
86
-
87
- <% end %>
88
90
 
89
91
  ```
90
92
 
91
-
93
+ <p id="notice"><%= notice %></p>
94
+
95
+
96
+
92
-
97
+ <h1>Syota</h1>
98
+
99
+
100
+
101
+ <table>
102
+
103
+ <thead>
104
+
105
+ <tr>
106
+
107
+ <th>From</th>
108
+
109
+ <th>Year</th>
110
+
111
+ <th>School</th>
112
+
113
+ <th>Seibetu</th>
114
+
115
+ <th colspan="3"></th>
116
+
117
+ </tr>
118
+
119
+ </thead>
120
+
121
+
122
+
123
+ <tbody>
124
+
125
+ <% @syota.each do |syotum| %>
126
+
127
+ <tr>
128
+
129
+ <td><%= syotum.from %></td>
130
+
131
+ <td><%= syotum.year %></td>
132
+
133
+ <td><%= syotum.school %></td>
134
+
135
+ <td><%= syotum.sex %></td>
136
+
137
+ <td><%= link_to 'Show', syotum %></td>
138
+
139
+ <td><%= link_to 'Edit', edit_syotum_path(syotum) %></td>
140
+
141
+ <td><%= link_to 'Destroy', syotum, method: :delete, data: { confirm: 'Are you sure?' } %></td>
142
+
143
+ </tr>
144
+
145
+ <% end %>
146
+
147
+ </tbody>
148
+
149
+ </table>
150
+
151
+
152
+
153
+ <br>
154
+
155
+
156
+
157
+ <%= link_to 'New Syotum', new_syotum_path %>
158
+
159
+ ``` 
160
+
161
+
162
+
93
- index.html.erb」
163
+ syota_controller.rb」
94
164
 
95
165
  ```
96
166
 
97
- <p id="notice"><%= notice %></p>
98
-
99
-
100
-
101
- <h1>Syota</h1>
102
-
103
-
104
-
105
- <table>
106
-
107
- <thead>
108
-
109
- <tr>
110
-
111
- <th>From</th>
112
-
113
- <th>Year</th>
114
-
115
- <th>School</th>
116
-
117
- <th>Seibetu</th>
118
-
119
- <th colspan="3"></th>
120
-
121
- </tr>
122
-
123
- </thead>
124
-
125
-
126
-
127
- <tbody>
128
-
129
- <% @syota.each do |syotum| %>
130
-
131
- <tr>
132
-
133
- <td><%= syotum.from %></td>
134
-
135
- <td><%= syotum.year %></td>
136
-
137
- <td><%= syotum.school %></td>
138
-
139
- <td><%= syotum.sex %></td>
140
-
141
- <td><%= link_to 'Show', syotum %></td>
142
-
143
- <td><%= link_to 'Edit', edit_syotum_path(syotum) %></td>
144
-
145
- <td><%= link_to 'Destroy', syotum, method: :delete, data: { confirm: 'Are you sure?' } %></td>
146
-
147
- </tr>
148
-
149
- <% end %>
150
-
151
- </tbody>
152
-
153
- </table>
154
-
155
-
156
-
157
- <br>
158
-
159
-
160
-
161
- <%= link_to 'New Syotum', new_syotum_path %>
162
-
163
- ``` 
164
-
165
-
166
-
167
- 「syota_controller.rb」
167
+ class SyotaController < ApplicationController
168
+
169
+ before_action :set_syotum, only: [:show, :edit, :update, :destroy]
170
+
171
+
172
+
173
+ # GET /syota
174
+
175
+ # GET /syota.json
176
+
177
+ def index
178
+
179
+ @syota = Syotum.all
180
+
181
+ end
182
+
183
+
184
+
185
+ # GET /syota/1
186
+
187
+ # GET /syota/1.json
188
+
189
+ def show
190
+
191
+ end
192
+
193
+
194
+
195
+ # GET /syota/new
196
+
197
+ def new
198
+
199
+ @syotum = Syotum.new
200
+
201
+ end
202
+
203
+
204
+
205
+ # GET /syota/1/edit
206
+
207
+ def edit
208
+
209
+ end
210
+
211
+
212
+
213
+ # POST /syota
214
+
215
+ # POST /syota.json
216
+
217
+ def create
218
+
219
+ @syotum = Syotum.new(syotum_params)
220
+
221
+
222
+
223
+ respond_to do |format|
224
+
225
+ if @syotum.save
226
+
227
+ format.html { redirect_to @syotum, notice: 'Syotum was successfully created.' }
228
+
229
+ format.json { render :show, status: :created, location: @syotum }
230
+
231
+ else
232
+
233
+ format.html { render :new }
234
+
235
+ format.json { render json: @syotum.errors, status: :unprocessable_entity }
236
+
237
+ end
238
+
239
+ end
240
+
241
+ end
242
+
243
+
244
+
245
+ # PATCH/PUT /syota/1
246
+
247
+ # PATCH/PUT /syota/1.json
248
+
249
+ def update
250
+
251
+ respond_to do |format|
252
+
253
+ if @syotum.update(syotum_params)
254
+
255
+ format.html { redirect_to @syotum, notice: 'Syotum was successfully updated.' }
256
+
257
+ format.json { render :show, status: :ok, location: @syotum }
258
+
259
+ else
260
+
261
+ format.html { render :edit }
262
+
263
+ format.json { render json: @syotum.errors, status: :unprocessable_entity }
264
+
265
+ end
266
+
267
+ end
268
+
269
+ end
270
+
271
+
272
+
273
+ # DELETE /syota/1
274
+
275
+ # DELETE /syota/1.json
276
+
277
+ def destroy
278
+
279
+ @syotum.destroy
280
+
281
+ respond_to do |format|
282
+
283
+ format.html { redirect_to syota_url, notice: 'Syotum was successfully destroyed.' }
284
+
285
+ format.json { head :no_content }
286
+
287
+ end
288
+
289
+ end
290
+
291
+
292
+
293
+ private
294
+
295
+ # Use callbacks to share common setup or constraints between actions.
296
+
297
+ def set_syotum
298
+
299
+ @syotum = Syotum.find(params[:id])
300
+
301
+ end
302
+
303
+
304
+
305
+ # Never trust parameters from the scary internet, only allow the white list through.
306
+
307
+ def syotum_params
308
+
309
+ params.require(:syotum).permit(:from, :year)
310
+
311
+ end
312
+
313
+ end
168
314
 
169
315
  ```
170
-
171
- class SyotaController < ApplicationController
172
-
173
- before_action :set_syotum, only: [:show, :edit, :update, :destroy]
174
-
175
-
176
-
177
- # GET /syota
178
-
179
- # GET /syota.json
180
-
181
- def index
182
-
183
- @syota = Syotum.all
184
-
185
- end
186
-
187
-
188
-
189
- # GET /syota/1
190
-
191
- # GET /syota/1.json
192
-
193
- def show
194
-
195
- end
196
-
197
-
198
-
199
- # GET /syota/new
200
-
201
- def new
202
-
203
- @syotum = Syotum.new
204
-
205
- end
206
-
207
-
208
-
209
- # GET /syota/1/edit
210
-
211
- def edit
212
-
213
- end
214
-
215
-
216
-
217
- # POST /syota
218
-
219
- # POST /syota.json
220
-
221
- def create
222
-
223
- @syotum = Syotum.new(syotum_params)
224
-
225
-
226
-
227
- respond_to do |format|
228
-
229
- if @syotum.save
230
-
231
- format.html { redirect_to @syotum, notice: 'Syotum was successfully created.' }
232
-
233
- format.json { render :show, status: :created, location: @syotum }
234
-
235
- else
236
-
237
- format.html { render :new }
238
-
239
- format.json { render json: @syotum.errors, status: :unprocessable_entity }
240
-
241
- end
242
-
243
- end
244
-
245
- end
246
-
247
-
248
-
249
- # PATCH/PUT /syota/1
250
-
251
- # PATCH/PUT /syota/1.json
252
-
253
- def update
254
-
255
- respond_to do |format|
256
-
257
- if @syotum.update(syotum_params)
258
-
259
- format.html { redirect_to @syotum, notice: 'Syotum was successfully updated.' }
260
-
261
- format.json { render :show, status: :ok, location: @syotum }
262
-
263
- else
264
-
265
- format.html { render :edit }
266
-
267
- format.json { render json: @syotum.errors, status: :unprocessable_entity }
268
-
269
- end
270
-
271
- end
272
-
273
- end
274
-
275
-
276
-
277
- # DELETE /syota/1
278
-
279
- # DELETE /syota/1.json
280
-
281
- def destroy
282
-
283
- @syotum.destroy
284
-
285
- respond_to do |format|
286
-
287
- format.html { redirect_to syota_url, notice: 'Syotum was successfully destroyed.' }
288
-
289
- format.json { head :no_content }
290
-
291
- end
292
-
293
- end
294
-
295
-
296
-
297
- private
298
-
299
- # Use callbacks to share common setup or constraints between actions.
300
-
301
- def set_syotum
302
-
303
- @syotum = Syotum.find(params[:id])
304
-
305
- end
306
-
307
-
308
-
309
- # Never trust parameters from the scary internet, only allow the white list through.
310
-
311
- def syotum_params
312
-
313
- params.require(:syotum).permit(:from, :year)
314
-
315
- end
316
-
317
- end
318
-
319
- ```

2

「View」「Controller」のコードの追記を行いましたので改善点がありましたご指摘の方お願いしたです。

2017/11/10 05:34

投稿

Syota
Syota

スコア28

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,10 @@
1
1
  railsを使い簡単なアプリの開発を進めているのですが、「Form」に入力したデータを反映させるにはどの様な操作を行えば反映させる事が出来ますか??
2
2
 
3
- viewの設定を行い項目名を表示する事は出来たのですが、入力したデータが反映されず空欄になってしまうので入力データを出力する設定の仕方を教えて頂きたいです。
3
+ viewの設定を行い項目名を表示する事は出来たのですが、入力したデータが反映されず空欄になってしまうので入力データを出力する設定の仕方を教えて頂きたいです。```ここに言語を入力
4
+
5
+ コード
6
+
7
+ ```
4
8
 
5
9
 
6
10
 
@@ -10,7 +14,9 @@
10
14
 
11
15
  「_form.html.erb」
12
16
 
17
+
18
+
13
- <%= form_with(model: syotum, local: true) do |form| %>
19
+ ```<%= form_with(model: syotum, local: true) do |form| %>
14
20
 
15
21
  <% if syotum.errors.any? %>
16
22
 
@@ -80,11 +86,13 @@
80
86
 
81
87
  <% end %>
82
88
 
89
+ ```
90
+
83
91
 
84
92
 
85
93
  「index.html.erb」
86
94
 
87
-
95
+ ```
88
96
 
89
97
  <p id="notice"><%= notice %></p>
90
98
 
@@ -152,11 +160,13 @@
152
160
 
153
161
  <%= link_to 'New Syotum', new_syotum_path %>
154
162
 
163
+ ``` 
164
+
155
165
 
156
166
 
157
167
  「syota_controller.rb」
158
168
 
159
-
169
+ ```
160
170
 
161
171
  class SyotaController < ApplicationController
162
172
 
@@ -305,3 +315,5 @@
305
315
  end
306
316
 
307
317
  end
318
+
319
+ ```

1

「view」,「controler」のコードの記載を追記しましたので改善点があればご指摘お願いします。

2017/11/10 05:33

投稿

Syota
Syota

スコア28

test CHANGED
File without changes
test CHANGED
@@ -5,3 +5,303 @@
5
5
 
6
6
 
7
7
  回答よろしくお願いします。
8
+
9
+
10
+
11
+ 「_form.html.erb」
12
+
13
+ <%= form_with(model: syotum, local: true) do |form| %>
14
+
15
+ <% if syotum.errors.any? %>
16
+
17
+ <div id="error_explanation">
18
+
19
+ <h2><%= pluralize(syotum.errors.count, "error") %> prohibited this syotum from being saved:</h2>
20
+
21
+
22
+
23
+ <ul>
24
+
25
+ <% syotum.errors.full_messages.each do |message| %>
26
+
27
+ <li><%= message %></li>
28
+
29
+ <% end %>
30
+
31
+ </ul>
32
+
33
+ </div>
34
+
35
+ <% end %>
36
+
37
+
38
+
39
+ <div class="field">
40
+
41
+ <%= form.label :from %>
42
+
43
+ <%= form.text_field :from, id: :syotum_from %>
44
+
45
+ </div>
46
+
47
+
48
+
49
+ <div class="field">
50
+
51
+ <%= form.label :year %>
52
+
53
+ <%= form.number_field :year, id: :syotum_year %>
54
+
55
+ </div>
56
+
57
+
58
+
59
+ <div class="field">
60
+
61
+ <%= form.label :school %>
62
+
63
+ <%= form.text_area :school, id: :syotum_school %>
64
+
65
+
66
+
67
+ <div class="field">
68
+
69
+ <%= form.label :seibetU %>
70
+
71
+ <%= form.text_field :sex, id: :syotum_sex %>
72
+
73
+
74
+
75
+ <div class="actions">
76
+
77
+ <%= form.submit %>
78
+
79
+ </div>
80
+
81
+ <% end %>
82
+
83
+
84
+
85
+ 「index.html.erb」
86
+
87
+
88
+
89
+ <p id="notice"><%= notice %></p>
90
+
91
+
92
+
93
+ <h1>Syota</h1>
94
+
95
+
96
+
97
+ <table>
98
+
99
+ <thead>
100
+
101
+ <tr>
102
+
103
+ <th>From</th>
104
+
105
+ <th>Year</th>
106
+
107
+ <th>School</th>
108
+
109
+ <th>Seibetu</th>
110
+
111
+ <th colspan="3"></th>
112
+
113
+ </tr>
114
+
115
+ </thead>
116
+
117
+
118
+
119
+ <tbody>
120
+
121
+ <% @syota.each do |syotum| %>
122
+
123
+ <tr>
124
+
125
+ <td><%= syotum.from %></td>
126
+
127
+ <td><%= syotum.year %></td>
128
+
129
+ <td><%= syotum.school %></td>
130
+
131
+ <td><%= syotum.sex %></td>
132
+
133
+ <td><%= link_to 'Show', syotum %></td>
134
+
135
+ <td><%= link_to 'Edit', edit_syotum_path(syotum) %></td>
136
+
137
+ <td><%= link_to 'Destroy', syotum, method: :delete, data: { confirm: 'Are you sure?' } %></td>
138
+
139
+ </tr>
140
+
141
+ <% end %>
142
+
143
+ </tbody>
144
+
145
+ </table>
146
+
147
+
148
+
149
+ <br>
150
+
151
+
152
+
153
+ <%= link_to 'New Syotum', new_syotum_path %>
154
+
155
+
156
+
157
+ 「syota_controller.rb」
158
+
159
+
160
+
161
+ class SyotaController < ApplicationController
162
+
163
+ before_action :set_syotum, only: [:show, :edit, :update, :destroy]
164
+
165
+
166
+
167
+ # GET /syota
168
+
169
+ # GET /syota.json
170
+
171
+ def index
172
+
173
+ @syota = Syotum.all
174
+
175
+ end
176
+
177
+
178
+
179
+ # GET /syota/1
180
+
181
+ # GET /syota/1.json
182
+
183
+ def show
184
+
185
+ end
186
+
187
+
188
+
189
+ # GET /syota/new
190
+
191
+ def new
192
+
193
+ @syotum = Syotum.new
194
+
195
+ end
196
+
197
+
198
+
199
+ # GET /syota/1/edit
200
+
201
+ def edit
202
+
203
+ end
204
+
205
+
206
+
207
+ # POST /syota
208
+
209
+ # POST /syota.json
210
+
211
+ def create
212
+
213
+ @syotum = Syotum.new(syotum_params)
214
+
215
+
216
+
217
+ respond_to do |format|
218
+
219
+ if @syotum.save
220
+
221
+ format.html { redirect_to @syotum, notice: 'Syotum was successfully created.' }
222
+
223
+ format.json { render :show, status: :created, location: @syotum }
224
+
225
+ else
226
+
227
+ format.html { render :new }
228
+
229
+ format.json { render json: @syotum.errors, status: :unprocessable_entity }
230
+
231
+ end
232
+
233
+ end
234
+
235
+ end
236
+
237
+
238
+
239
+ # PATCH/PUT /syota/1
240
+
241
+ # PATCH/PUT /syota/1.json
242
+
243
+ def update
244
+
245
+ respond_to do |format|
246
+
247
+ if @syotum.update(syotum_params)
248
+
249
+ format.html { redirect_to @syotum, notice: 'Syotum was successfully updated.' }
250
+
251
+ format.json { render :show, status: :ok, location: @syotum }
252
+
253
+ else
254
+
255
+ format.html { render :edit }
256
+
257
+ format.json { render json: @syotum.errors, status: :unprocessable_entity }
258
+
259
+ end
260
+
261
+ end
262
+
263
+ end
264
+
265
+
266
+
267
+ # DELETE /syota/1
268
+
269
+ # DELETE /syota/1.json
270
+
271
+ def destroy
272
+
273
+ @syotum.destroy
274
+
275
+ respond_to do |format|
276
+
277
+ format.html { redirect_to syota_url, notice: 'Syotum was successfully destroyed.' }
278
+
279
+ format.json { head :no_content }
280
+
281
+ end
282
+
283
+ end
284
+
285
+
286
+
287
+ private
288
+
289
+ # Use callbacks to share common setup or constraints between actions.
290
+
291
+ def set_syotum
292
+
293
+ @syotum = Syotum.find(params[:id])
294
+
295
+ end
296
+
297
+
298
+
299
+ # Never trust parameters from the scary internet, only allow the white list through.
300
+
301
+ def syotum_params
302
+
303
+ params.require(:syotum).permit(:from, :year)
304
+
305
+ end
306
+
307
+ end