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

質問編集履歴

4

コントローラの内容を追記しました。

2017/10/19 08:03

投稿

koume
koume

スコア458

title CHANGED
File without changes
body CHANGED
@@ -20,42 +20,7 @@
20
20
  ・更新は update アクション
21
21
  コードは以下になります。
22
22
 
23
- コントローラ
24
23
  ```ここに言語を入力
25
- def edit
26
- @bank_form = Customer::BankForm.new(Bank.find_by(customer_id: current_customer.id))
27
- end
28
-
29
- # PATCH
30
- def confirm
31
- @bank_form = Customer::BankForm.new(Bank.find_by(customer_id: current_customer.id))
32
- @bank_form.assign_attributes(bank_params)
33
- if @bank_form.valid?
34
- render action: 'confirm'
35
- else
36
- flash.now.alert = '入力に誤りがあります。'
37
- render action: 'edit'
38
- end
39
- end
40
-
41
- def update
42
- @bank_form = Customer::BankForm.new(Bank.find_by(customer_id: current_customer.id))
43
- @bank_form.assign_attributes(bank_params)
44
- if params[:commit]
45
- if @bank_form.save
46
- flash.notice = '口座アカウントを更新しました。'
47
- redirect_to :customer_root
48
- else
49
- flash.now.alert = '入力に誤りがあります。'
50
- render action: 'edit'
51
- end
52
- else
53
- render action: 'edit'
54
- end
55
- end
56
-
57
- ```
58
- ```ここに言語を入力
59
24
  edit.html.erb
60
25
 
61
26
  <% @title = '口座アカウントの編集' %>
@@ -204,20 +169,8 @@
204
169
  ZXWPIvFui2YXpqKCfbAUqls2Yob43UadjW4nw7tfWWIupT+5v/+BCNv4Q==", "form"=>{"bank"=>{
205
170
  "bk_name"=>"xyz銀行", "office"=>"いろは支店", "account"=>"1234567", "acc_name"=>
206
171
  "佐藤一郎", "acc_name_kana"=>"サトウイチロウ"}}, "correct"=>"訂正", "host"=>"exa mple.com"}
207
- Customer Load (0.5ms) SELECT `customers`.* FROM `customers` WHERE `customers
172
+
208
- `.`id` = 1 LIMIT 1
209
- Bank Load (1.3ms) SELECT `banks`.* FROM `banks` WHERE `banks`.`customer_id`
210
- = 1 LIMIT 1
211
- Rendering customer/banks/edit.html.erb within layouts/customer
212
- Rendered customer/banks/_bank_fields.html.erb (14.1ms)
213
- Rendered customer/banks/_form.html.erb (16.0ms)
214
- Rendered customer/banks/edit.html.erb within layouts/customer (18.3ms)
215
- Rendered customer/shared/_header.html.erb (1.1ms)
216
- Rendered shared/_footer.html.erb (0.3ms)
217
- Completed 200 OK in 327ms (Views: 321.2ms | ActiveRecord: 1.8ms)
218
173
 
219
-
220
-
221
174
  confirm.html.erbの表示されたときの出力
222
175
 
223
176
  Started PATCH "/mypage/bank/confirm" for 10.0.2.2 at 2017-10-18 12:23:52 +0000
@@ -227,17 +180,47 @@
227
180
  "bk_name"=>"xyz銀行fd", "office"=>"いろは支店", "account"=>"1234567", "acc_name"
228
181
  =>"佐藤一郎", "acc_name_kana"=>"サトウイチロウ"}}, "commit"=>"確認画面へ進む", "
229
182
  host"=>"example.com"}
230
- Customer Load (0.5ms) SELECT `customers`.* FROM `customers` WHERE `customers
183
+
231
- `.`id` = 1 LIMIT 1
184
+ ```
232
- Bank Load (1.1ms) SELECT `banks`.* FROM `banks` WHERE `banks`.`customer_id`
233
- = 1 LIMIT 1
234
- Rendering customer/banks/confirm.html.erb within layouts/customer
235
- Rendered customer/banks/_bank_fields.html.erb (13.0ms)
236
- Rendered customer/banks/_confirming_form.html.erb (14.5ms)
237
- Rendered customer/banks/confirm.html.erb within layouts/customer (16.4ms)
238
- Rendered customer/shared/_header.html.erb (0.9ms)
185
+ bk_nameを編集した時の出力です。これを見る限りデータは送られてきているのかな?って思いますが表示は編集前の値です。
239
- Rendered shared/_footer.html.erb (0.0ms)
240
- Completed 200 OK in 312ms (Views: 305.7ms | ActiveRecord: 1.5ms)
241
186
 
187
+
188
+ 追記
189
+ ```ここに言語を入力
190
+ def confirm
191
+  @bank_form = Customer::BankForm.new(Bank.find_by(customer_id: current_customer.id))
192
+ @bank_form.assign_attributes(params[:form][:bank][:bk_name])
193
+ @bank_form.assign_attributes(params[:form][:bank][:office])
194
+ @bank_form.assign_attributes(params[:form][:bank][:account])
195
+ @bank_form.assign_attributes(params[:form][:bank][:acc_name])
196
+ @bank_form.assign_attributes(params[:form][:bank][:acc_name_kana])
197
+ if @bank_form.valid?
198
+ render action: 'confirm'
199
+ else
200
+ flash.now.alert = '入力に誤りがあります。'
201
+ render action: 'edit'
202
+ end
203
+ end
204
+
205
+ def create
206
+ end
207
+
208
+ def update
209
+ @bank_form = Customer::BankForm.new(Bank.find_by(customer_id: current_customer.id))
210
+ @bank_form.assign_attributes(params[:form][:bank][:bk_name])
211
+ @bank_form.assign_attributes(params[:form][:bank][:office])
212
+ @bank_form.assign_attributes(params[:form][:bank][:account])
213
+ @bank_form.assign_attributes(params[:form][:bank][:acc_name])
214
+ @bank_form.assign_attributes(params[:form][:bank][:acc_name_kana])
215
+ if params[:commit]
216
+ if @bank_form.save
217
+ flash.notice = '口座アカウントを更新しました。'
218
+ redirect_to :customer_root
219
+ else
220
+ flash.now.alert = '入力に誤りがあります。'
221
+ render action: 'edit'
222
+ end
223
+ else
224
+ render action: 'edit'
225
+ end
242
- ```
226
+ ```
243
- bk_nameを編集した時の出力です。これを見る限りデータは送られてきているのかな?って思いますが表示は編集前の値です。

3

コンソールの内容を追記

2017/10/19 08:03

投稿

koume
koume

スコア458

title CHANGED
File without changes
body CHANGED
@@ -193,4 +193,51 @@
193
193
  end %>
194
194
  <% end %>
195
195
 
196
- ```
196
+ ```
197
+ 追記
198
+ ```ここに言語を入力
199
+ edit.html.erbが表示されたときの出力
200
+
201
+ Started PATCH "/mypage/bank" for 10.0.2.2 at 2017-10-18 12:23:34 +0000
202
+ Processing by Customer::BanksController#update as HTML
203
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"1chV0w0pxy53uFHxymlrUDzu1aIlq
204
+ ZXWPIvFui2YXpqKCfbAUqls2Yob43UadjW4nw7tfWWIupT+5v/+BCNv4Q==", "form"=>{"bank"=>{
205
+ "bk_name"=>"xyz銀行", "office"=>"いろは支店", "account"=>"1234567", "acc_name"=>
206
+ "佐藤一郎", "acc_name_kana"=>"サトウイチロウ"}}, "correct"=>"訂正", "host"=>"exa mple.com"}
207
+ Customer Load (0.5ms) SELECT `customers`.* FROM `customers` WHERE `customers
208
+ `.`id` = 1 LIMIT 1
209
+ Bank Load (1.3ms) SELECT `banks`.* FROM `banks` WHERE `banks`.`customer_id`
210
+ = 1 LIMIT 1
211
+ Rendering customer/banks/edit.html.erb within layouts/customer
212
+ Rendered customer/banks/_bank_fields.html.erb (14.1ms)
213
+ Rendered customer/banks/_form.html.erb (16.0ms)
214
+ Rendered customer/banks/edit.html.erb within layouts/customer (18.3ms)
215
+ Rendered customer/shared/_header.html.erb (1.1ms)
216
+ Rendered shared/_footer.html.erb (0.3ms)
217
+ Completed 200 OK in 327ms (Views: 321.2ms | ActiveRecord: 1.8ms)
218
+
219
+
220
+
221
+ confirm.html.erbの表示されたときの出力
222
+
223
+ Started PATCH "/mypage/bank/confirm" for 10.0.2.2 at 2017-10-18 12:23:52 +0000
224
+ Processing by Customer::BanksController#confirm as HTML
225
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"hiE+QpQjqQQexMF03G2OxsTuf17oA
226
+ dwxcT8npHiH3HPZ4J1Ry6MC8+Nnc/AMctAuZw5Hgagg83OzUh3gUTztCA==", "form"=>{"bank"=>{
227
+ "bk_name"=>"xyz銀行fd", "office"=>"いろは支店", "account"=>"1234567", "acc_name"
228
+ =>"佐藤一郎", "acc_name_kana"=>"サトウイチロウ"}}, "commit"=>"確認画面へ進む", "
229
+ host"=>"example.com"}
230
+ Customer Load (0.5ms) SELECT `customers`.* FROM `customers` WHERE `customers
231
+ `.`id` = 1 LIMIT 1
232
+ Bank Load (1.1ms) SELECT `banks`.* FROM `banks` WHERE `banks`.`customer_id`
233
+ = 1 LIMIT 1
234
+ Rendering customer/banks/confirm.html.erb within layouts/customer
235
+ Rendered customer/banks/_bank_fields.html.erb (13.0ms)
236
+ Rendered customer/banks/_confirming_form.html.erb (14.5ms)
237
+ Rendered customer/banks/confirm.html.erb within layouts/customer (16.4ms)
238
+ Rendered customer/shared/_header.html.erb (0.9ms)
239
+ Rendered shared/_footer.html.erb (0.0ms)
240
+ Completed 200 OK in 312ms (Views: 305.7ms | ActiveRecord: 1.5ms)
241
+
242
+ ```
243
+ bk_nameを編集した時の出力です。これを見る限りデータは送られてきているのかな?って思いますが表示は編集前の値です。

2

_bank_fields.html.erbを追記しました。

2017/10/18 12:41

投稿

koume
koume

スコア458

title CHANGED
File without changes
body CHANGED
@@ -175,4 +175,22 @@
175
175
  def assign_attributes(params = {})
176
176
  @params = params
177
177
  end
178
+ ```
179
+ 追記
180
+ ```ここに言語を入力
181
+ _bank_fields.html.erb
182
+
183
+ <%= f.fields_for :bank, f.object.bank do |ff| %>
184
+ <%= markup do |m|
185
+ p = confirming ? ConfirmingBankFormPresenter.new(ff, self) : BankFormPresenter.new(ff, self)
186
+ p.with_options(required: true) do |q|
187
+ m << q.bk_name_block(:bk_name, '銀行名')
188
+ m << q.office_block(:office, '支店名')
189
+ m << q.account_block(:account, '口座番号')
190
+ m << q.acc_name_block(:acc_name, '口座名義')
191
+ m << q.acc_name_kana_block(:acc_name_kana, '口座名義(フリガナ)')
192
+ end
193
+ end %>
194
+ <% end %>
195
+
178
196
  ```

1

コードの記述忘れがありました。

2017/10/18 09:28

投稿

koume
koume

スコア458

title CHANGED
File without changes
body CHANGED
@@ -154,4 +154,25 @@
154
154
  end
155
155
  ```
156
156
  以上ですが、confirming_bank_form_presenter.rb の m.div(object.send(name), class: 'field-value') のコードで確認画面にデータが表示されているのですが、編集した内容ではなく初期データが表示されてしまいます。どう記述すれば「編集」画面の内容を表示できるのでしょうか?
157
- どなたか教えてください。宜しくお願いします。
157
+ どなたか教えてください。宜しくお願いします。
158
+
159
+ 追記
160
+ ```ここに言語を入力
161
+ bank_form.rb
162
+
163
+ class Customer::BankForm
164
+ include ActiveModel::Model
165
+
166
+ attr_accessor :bank
167
+ delegate :persisted?, :valid?, :save, to: :bank
168
+
169
+ def initialize(bank = nil)
170
+ @bank = bank
171
+ @bank ||= Bank.new
172
+ @customer.build_bank unless @bank
173
+ end
174
+
175
+ def assign_attributes(params = {})
176
+ @params = params
177
+ end
178
+ ```