質問編集履歴
2
Routesを追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -64,7 +64,7 @@
|
|
64
64
|
<%= @item.send_fee.name %>
|
65
65
|
</p>
|
66
66
|
|
67
|
-
<%= form_with model: @order_address,url: item_orders_path
|
67
|
+
<%= form_with model: @order_address,url: item_orders_path, id: 'charge-form',local: true do |f| %>
|
68
68
|
|
69
69
|
<%= render 'shared/error_messages', model: f.object %>
|
70
70
|
|
@@ -158,17 +158,15 @@
|
|
158
158
|
<%= f.text_field :phone_number, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%>
|
159
159
|
</div>
|
160
160
|
|
161
|
-
<div class="
|
161
|
+
<div class="btn-contents">
|
162
|
-
<%=
|
162
|
+
<%= f.submit "購入する", class:'submit-btn' %>
|
163
163
|
</div>
|
164
|
-
|
165
|
-
<% end %>
|
166
|
-
</div>
|
167
|
-
|
168
164
|
<div>
|
169
165
|
<%=link_to 'もどる', :back, class:"back-btn" %>
|
170
166
|
</div>
|
171
167
|
|
168
|
+
<% end %>
|
169
|
+
</div>
|
172
170
|
</div>
|
173
171
|
</div>
|
174
172
|
```
|
@@ -253,4 +251,22 @@
|
|
253
251
|
end
|
254
252
|
|
255
253
|
end
|
254
|
+
```
|
255
|
+
|
256
|
+
### Routes(追記)
|
257
|
+
```ruby
|
258
|
+
Rails.application.routes.draw do
|
259
|
+
devise_for :users, controllers: { registrations: 'users/registrations', sessions: 'users/sessions' }
|
260
|
+
root to: 'users#new'
|
261
|
+
resources :users, only: %i[show new]
|
262
|
+
|
263
|
+
resources :items, only: %i[index new create show destroy] do
|
264
|
+
resources :orders, only: %i[index create]
|
265
|
+
end
|
266
|
+
|
267
|
+
resources :babies, only: %i[new create] do
|
268
|
+
resources :articles, only: %i[index show new create destroy]
|
269
|
+
end
|
270
|
+
|
271
|
+
end
|
256
272
|
```
|
1
Controllerを追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -210,4 +210,47 @@
|
|
210
210
|
### 試したこと
|
211
211
|
|
212
212
|
<%= %>のイコールが抜けていないことは確認しました。
|
213
|
-
意図的にバリデーションエラーが発生する状況で試しましたが、これでもうまく表示されません。
|
213
|
+
意図的にバリデーションエラーが発生する状況で試しましたが、これでもうまく表示されません。
|
214
|
+
|
215
|
+
### Controller(追記)
|
216
|
+
|
217
|
+
|
218
|
+
```Ruby
|
219
|
+
class OrdersController < ApplicationController
|
220
|
+
|
221
|
+
def index
|
222
|
+
@item = Item.find(params[:item_id])
|
223
|
+
@order_address = OrderAddress.new
|
224
|
+
end
|
225
|
+
|
226
|
+
def create
|
227
|
+
@item = Item.find(params[:item_id])
|
228
|
+
@order_address = OrderAddress.new(order_params)
|
229
|
+
if @order_address.valid?
|
230
|
+
pay_item
|
231
|
+
@order_address.save
|
232
|
+
return redirect_to user_path(current_user.id)
|
233
|
+
else
|
234
|
+
@order_address = OrderAddress.new(order_params)
|
235
|
+
render :index
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
|
240
|
+
private
|
241
|
+
|
242
|
+
def order_params
|
243
|
+
params.require(:order_address).permit(:zip_code, :prefecture_id, :city, :address, :building_name, :phone_number).merge(item_id: params[:item_id],user_id: current_user.id,token: params[:token])
|
244
|
+
end
|
245
|
+
|
246
|
+
def pay_item
|
247
|
+
Payjp.api_key = ENV['PAYJP_SECRET_KEY']
|
248
|
+
Payjp::Charge.create(
|
249
|
+
amount: @item.price,
|
250
|
+
card: order_params[:token],
|
251
|
+
currency: 'jpy'
|
252
|
+
)
|
253
|
+
end
|
254
|
+
|
255
|
+
end
|
256
|
+
```
|