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

質問編集履歴

1

貼り付けるコードを間違えていました。

2022/10/19 13:48

投稿

pi-nattu
pi-nattu

スコア61

title CHANGED
File without changes
body CHANGED
@@ -33,100 +33,80 @@
33
33
  ※current_idなどを入れてみたりしましたがそれも同じくエラーが出てしまいました。
34
34
  ### 補足情報(FW/ツールのバージョンなど)
35
35
  ```ruby_on_rails
36
- class Public::CartItemsController < ApplicationController
36
+ class Public::AddressesController < ApplicationController
37
-
38
37
  def index
39
- @cart_items = CartItem.all
40
- @items = Item.all
41
- @cart_item = @items.all
38
+ @address_new = Address.new
42
- #合計金額の初期値は0円
43
- @total = 0
39
+ @addresses = current_customer.addresses.all
44
40
  end
45
41
 
42
+ def create
43
+ @address_new = Address.new(address_params)
44
+ @address_new.customer_id = current_customer.id
45
+ @address_new.save
46
+ redirect_to addresses_index_path
47
+ end
46
48
 
47
- def create
48
- @cart_item = CartItem.find_by(item_id: params[:cart_item][:item_id])
49
- if @cart_item
50
- #カートにitemが存在したらamountに新しいCart_itemのamountを足す
51
- @cart_item.amount += CartItem.new(cart_item_params).amount
52
- else
53
- #カートにitemが無ければ新しく作成する
54
- @cart_item = CartItem.new(cart_item_params)
55
- end
56
- #ログインcustomerのみ更新できるようにするため分岐の外に記述する
57
- if @cart_item.customer = current_customer
58
- @cart_item.save!
59
- redirect_to cart_items_path
60
- else
61
- redirect_to new_customer_session_path
62
- end
63
- end
64
-
65
-
66
49
  def update
67
- @cart_item = CartItem.find(params[:id])
68
- @item = Item.all
69
- @cart_item.update(cart_item_params)
70
- redirect_to cart_items_path
71
50
  end
51
+
52
+ def edit
53
+ end
72
54
 
55
+
73
56
  def destroy
74
- @cart_item = CartItem.find(params[:id])
57
+ @addresses = Address.find(params[:address_id])
75
- @cart_item.destroy!
58
+ @addresses.destroy
76
- redirect_to cart_items_path
59
+ redirect_to addresses_index_path
77
60
  end
78
61
 
79
- def destroy_all
62
+ def address_params
80
- current_customer.cart_items.destroy_all
63
+ params.require(:address).permit(:customer_id, :name, :postal_code, :address)
81
- redirect_to cart_items_path
82
64
  end
83
-
84
- private
85
- def cart_item_params
86
- params.require(:cart_item).permit(:item_id, :amount)
87
- end
88
65
  end
89
66
 
90
-
91
67
  ```
92
68
  ↓は自分の住所や名前の表示と新規配送先登録を兼ねたものになります。
93
69
  indexとnewを同じビューで扱っており、念のため全体のコードを貼っています。
94
70
  ```ruby_on_rails
95
- <div>
96
- <h1>支払い方法</h1>
71
+ <h1>自分の住所</h1>
97
- <%= form_with model: @order, url: orders_comfirm_path do |f| %>
98
- <%= f.radio_button :payment_method, Order.payment_methods.key(0) %>
99
- <%= f.label :payment_method, Order.payment_methods_i18n[:credit_card] %>
72
+ <h4>氏名</h4><%= current_customer.last_name %> <%= current_customer.first_name %><br>
100
-
101
-
102
- <%= f.radio_button :payment_method, Order.payment_methods.key(1) %>
103
- <%= f.label :payment_method, Order.payment_methods_i18n[:transfer] %>
104
-
105
- <h2>お届け先</h2>
106
- <%#=自分の住所%>
107
- <%= f.radio_button :delivery_method, Order.delivery_methods.key(0) %>
108
- <%= f.label :delivery_method, Order.delivery_methods_i18n[:my_postal_code] %><br>
73
+ <%= current_customer.last_name_kana %> <%= current_customer.first_name_kana %><br>
109
- <%= current_customer.postal_code %><br><%= current_customer.address %><br>
74
+ <h4>住所</h4><%= current_customer.postal_code %><br><%= current_customer.address %><br>
75
+ <%= link_to "編集する", addresses_edit_path %>
110
76
 
77
+ <%= form_with model: @address_new, url: addresses_create_path, method: :post do |f| %>
78
+ <%= f.label :郵便番号(ハイフンなし)%><%= f.text_field :postal_code %><br>
79
+ <%= f.label :住所 %><%= f.text_field :address %><br>
80
+ <%= f.label :氏名 %><%= f.text_field :name %><br>
81
+ <%= f.submit "新規登録" %>
82
+ <% end %>
111
83
 
112
- <%#=登録済み住所から選択%>
113
- <%= f.radio_button :delivery_method, Order.delivery_methods.key(1) %>
114
- <%= f.label :delivery_method, Order.delivery_methods_i18n[:registered_address] %><br>
115
- <%= f.select :address_id, options_from_collection_for_select(Address.all, :id, :address_display) %><br>
116
-
117
-
118
- <%#= 新しい敗走先を設定する%>
119
- <%= f.radio_button :delivery_method, Order.delivery_methods.key(2) %>
120
- <%= f.label :delivery_method, Order.delivery_methods_i18n[:new_address] %><br>
121
-
122
- <h4>新しい配送先を指定してください</h4>
123
- <p>宛名<%= f.text_field :delivery_name %></p>
124
- <p>※ハイフン無し</p>
84
+ <h4>お届け先一覧</h4>
85
+ <div>
86
+ <table>
87
+ <thead>
88
+ <tr>
89
+ <th>郵便番号</th>
90
+ <th>住所</th>
91
+ <th>氏名</th>
92
+ <th></th>
93
+ </tr>
94
+ </thead>
95
+ <tbody>
96
+ <% @addresses.each do |addresses| %>
97
+ <tr>
125
- <p><%= f.text_field :delivery_code %></p>
98
+ <td><%= addresses.postal_code %></td><br>
126
- <p>住所<%= f.text_area :delivery_address %></p><br>
99
+ <td><%= addresses.address %></td><br>
127
-
100
+ <td><%= addresses.name %></td><br>
101
+ <td>
102
+ <%= link_to "編集する", addresses_edit_path %>
103
+ <%= link_to "削除する", addresses_destroy_path, method: :delete,
128
- <%= f.submit '確認画面へ進む' %>
104
+ "data-confirm" => "本当に削除しますか?" %>
105
+ </td>
106
+ </tr>
129
- <% end %>
107
+ <% end %>
108
+ </tbody>
109
+ </table>
130
110
  </div>
131
111
  ```
132
112
  ```ruby_on_rails/routing