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

質問編集履歴

4

formオブジェクトの記述を変えてみました。

2020/12/10 09:42

投稿

murohi-08
murohi-08

スコア12

title CHANGED
File without changes
body CHANGED
@@ -187,7 +187,7 @@
187
187
  以下formオブジェクトのモデルファイルになります。
188
188
 
189
189
  ```ここに言語を入力
190
- app/models/user_item.rb
190
+ app/models/user_purchase_history.rb
191
191
 
192
192
  class UserPurchaseHistory
193
193
  include ActiveModel::Model

3

formオブジェクトの記述を変えてみました。

2020/12/10 09:42

投稿

murohi-08
murohi-08

スコア12

title CHANGED
File without changes
body CHANGED
@@ -6,6 +6,7 @@
6
6
  ```
7
7
 
8
8
  ```ここに言語を入力
9
+
9
10
  app/controllers/purchase_histories_controller.rb
10
11
 
11
12
  class PurchaseHistoriesController < ApplicationController
@@ -32,8 +33,8 @@
32
33
  def purchase_history_params
33
34
  params.require(:user_purchase_history).permit(:nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana)
34
35
  end
35
- end```
36
+ end
36
-
37
+ ```
37
38
  ```ここに言語を入力
38
39
  views/parchase_history/index.html.erb
39
40
 

2

formオブジェクトの記述を変えてみました。

2020/12/10 07:53

投稿

murohi-08
murohi-08

スコア12

title CHANGED
File without changes
body CHANGED
@@ -168,6 +168,8 @@
168
168
  </div>
169
169
  <%= render "shared/second-footer"%>
170
170
  ```
171
+
172
+
171
173
  ```ここに言語を入力
172
174
  config/routes.rb
173
175
 

1

formオブジェクトの記述を変えてみました。

2020/12/10 07:51

投稿

murohi-08
murohi-08

スコア12

title CHANGED
File without changes
body CHANGED
@@ -10,17 +10,17 @@
10
10
 
11
11
  class PurchaseHistoriesController < ApplicationController
12
12
  def index
13
- @user_items = UserItem.all #この部分
13
+ @user_purchase_histories = UserPurchaseHistory.all
14
14
  end
15
15
 
16
16
  def new
17
- @user_item = UserItem.new
17
+ @user_purchase_history = UserPurchaseHistory.new
18
18
  end
19
19
 
20
20
  def create
21
- @user_item = UserItem.new(purchase_history_params)
21
+ @user_purchase_history = UserPurchaseHistory.new(purchase_history_params)
22
- if @user_item.valid?
22
+ if @user_purchase_history.valid?
23
- @user_item.save
23
+ @user_purchase_history.save
24
24
  redirect_to action: :index
25
25
  else
26
26
  render action: :new
@@ -30,10 +30,9 @@
30
30
  private
31
31
 
32
32
  def purchase_history_params
33
- params.require(:user_item).permit(:nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana, :name, :introduction, :category, :item_condition, :postage_id, :prefecture_id, :prepare_id, :price)
33
+ params.require(:user_purchase_history).permit(:nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana)
34
34
  end
35
- end
35
+ end```
36
- ```
37
36
 
38
37
  ```ここに言語を入力
39
38
  views/parchase_history/index.html.erb
@@ -71,7 +70,7 @@
71
70
  </div>
72
71
  <%# /支払額の表示 %>
73
72
 
74
- <%= form_with model: @user_items, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>
73
+ <%= form_with model: @user_purchase_history, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>
75
74
  <%# カード情報の入力 %>
76
75
  <div class='credit-card-form'>
77
76
  <h1 class='info-input-haedline'>
@@ -169,14 +168,27 @@
169
168
  </div>
170
169
  <%= render "shared/second-footer"%>
171
170
  ```
171
+ ```ここに言語を入力
172
+ config/routes.rb
173
+
174
+ Rails.application.routes.draw do
175
+ devise_for :users
176
+ root "items#index"
177
+ resources :items do
178
+ resources :purchase_histories, only: [:index, :new, :create]
179
+ end
180
+ end
181
+ ```
182
+
183
+
172
184
  以下formオブジェクトのモデルファイルになります。
173
185
 
174
186
  ```ここに言語を入力
175
187
  app/models/user_item.rb
176
188
 
177
- class UserItem
189
+ class UserPurchaseHistory
178
190
  include ActiveModel::Model
179
- attr_accessor :nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana, :name, :introduction, :category, :item_condition, :postage_id, :prefecture_id, :prepare_id, :price
191
+ attr_accessor :nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana, :birth_day
180
192
 
181
193
  with_options presence: true do
182
194
  validates :nickname
@@ -187,38 +199,13 @@
187
199
  validates :family_name, format: { with: /\A[ぁ-んァ-ン一-龥]/, message: "Full-width characters."}
188
200
  validates :family_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "Full-width katakana characters."}
189
201
  validates :birth_day
190
- validates :image
191
- validates :name
192
- validates :introduction
193
- validates :price
194
202
  end
195
- validates :price, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9999999, message: "Out of setting range"}
196
- validates :price, numericality: { only_integer: true, message: "Half-width number." }
197
-
198
- with_options numericality: { other_than: 0, message: "Select" } do
199
- validates :item_condition_id
200
- validates :postage_id
201
- validates :prefecture_id
202
- validates :prepare_id
203
- validates :category_id
204
- end
205
203
 
206
204
  def save
207
205
  user = User.create(nickname: nickname, email: email, password: password, first_name: first_name, family_name: family_name, first_name_kana: first_name_kana, family_name_kana: family_name_kana, birth_day: birth_day)
208
- Item.create(name: name, introduction: introduction, category_id: category.id, item_condition_id: item_condition.id, postage_id: postage.id, prefecture_id: prefecture.id, prepare_id: prepare.id, price: price, user_id: user.id)
206
+ SendingDestination.create(post_code: post_code, prefecture_id: prefecture.id, city: city, house_number: house_number, building_name: building_name, phone_number: phone_number, purchase_history_id: purchase_history.id)
207
+ Purchase_history.create(user_id: user.id, item_id: item.id)
209
208
  end
210
209
 
211
210
  end
212
- ```
213
-
214
- ```ここに言語を入力
215
- config/routes.rb
216
-
217
- Rails.application.routes.draw do
218
- devise_for :users
219
- root "items#index"
220
- resources :items do
221
- resources :purchase_histories, only: [:index, :new, :create]
222
- end
223
- end
224
211
  ```