質問編集履歴

2

変更点

2020/10/25 03:11

投稿

tomato185
tomato185

スコア29

test CHANGED
File without changes
test CHANGED
@@ -209,3 +209,71 @@
209
209
 
210
210
 
211
211
  ```
212
+
213
+
214
+
215
+ ```
216
+
217
+ farmers_Controller.rb
218
+
219
+ class FarmersController < ApplicationController
220
+
221
+
222
+
223
+ def new
224
+
225
+ @farmer_resister = Farmer.new
226
+
227
+ @farmer_resister.farmer_images.build
228
+
229
+ end
230
+
231
+
232
+
233
+ def create
234
+
235
+ @farmer_resister = Farmer.new(farmer_params)
236
+
237
+ if @farmer_resister.save!
238
+
239
+ farmer_log_in @farmer_resister
240
+
241
+ flash.now[:success] = "農家の登録が完了しました。"
242
+
243
+ redirect_to farmer_path(@farmer_resister.id)
244
+
245
+ else
246
+
247
+ flash.now[:danger] = "農家登録に失敗しました。お手数ですが、もう一度登録しなおしてください。"
248
+
249
+ render "new"
250
+
251
+ end
252
+
253
+ end
254
+
255
+
256
+
257
+
258
+
259
+ private
260
+
261
+
262
+
263
+ def farmer_params
264
+
265
+ params.require(:farmer).permit(:name, :email, :tel, :postal_code, :prefecture, :address, :comment, :password, :password_confirmation, farmer_images_attributes:
266
+
267
+ [
268
+
269
+ :id, :farmer_logo, :farmer_image
270
+
271
+ ])
272
+
273
+ end
274
+
275
+ end
276
+
277
+
278
+
279
+ ```

1

追加

2020/10/25 03:11

投稿

tomato185
tomato185

スコア29

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  ```
22
22
 
23
- 都道府県と住所が空だよ。
23
+ Validation failed: Prefecture can't be blank, Address can't be blank.
24
24
 
25
25
 
26
26
 
@@ -42,6 +42,36 @@
42
42
 
43
43
 
44
44
 
45
+ ```
46
+
47
+ ログ
48
+
49
+
50
+
51
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"M7JNCtF75uixj6s6kw8pZk0ChuDN4N8Ev8f1I0mYt0x75HyDOi1fG/EWf36RT8y2NKrxgMh5J7+JnLNeh0SqRA==", "farmer"=>{"name"=>"XXX農園", "email"=>"ccc@ccc.com", "tel"=>"00000000000", "postal_code"=>"0608588", "comment"=>"テストです。", "farmer_images_attributes"=>{"0"=>{"farmer_logo"=>#<ActionDispatch::Http::UploadedFile:0x00007fd6e90f76a8 @tempfile=#<Tempfile:/var/folders/19/8dw27p954zv9p2k_90hvh3ww0000gn/T/RackMultipart20201025-19521-ckzet6.png>, @original_filename="lemon.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"farmer[farmer_images_attributes][0][farmer_logo]\"; filename=\"lemon.png\"\r\nContent-Type: image/png\r\n">, "farmer_image"=>#<ActionDispatch::Http::UploadedFile:0x00007fd6e90f7360 @tempfile=#<Tempfile:/var/folders/19/8dw27p954zv9p2k_90hvh3ww0000gn/T/RackMultipart20201025-19521-h9zj8i.png>, @original_filename="apple.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"farmer[farmer_images_attributes][0][farmer_image]\"; filename=\"apple.png\"\r\nContent-Type: image/png\r\n">}}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "prefecture"=>"北海道", "address"=>"札幌市中央区", "commit"=>"登録"}
52
+
53
+
54
+
55
+ (0.1ms) begin transaction
56
+
57
+ ↳ app/controllers/farmers_controller.rb:10
58
+
59
+ Farmer Exists (0.3ms) SELECT 1 AS one FROM "farmers" WHERE LOWER("farmers"."email") = LOWER(?) LIMIT ? [["email", "ccc@ccc.com"], ["LIMIT", 1]]
60
+
61
+ ↳ app/controllers/farmers_controller.rb:10
62
+
63
+ (0.2ms) rollback transaction
64
+
65
+ ↳ app/controllers/farmers_controller.rb:10
66
+
67
+ Completed 422 Unprocessable Entity in 142ms (ActiveRecord: 2.2ms)
68
+
69
+
70
+
71
+ ```
72
+
73
+
74
+
45
75
 
46
76
 
47
77
  ### 該当のソースコード
@@ -50,7 +80,7 @@
50
80
 
51
81
  ```
52
82
 
53
- (views/products/new.html.erb)一部記載
83
+ (views/farmers/new.html.erb)一部記載
54
84
 
55
85
  ここで登録したい商品の情報を記入(下記はタグを複数選択可)
56
86
 
@@ -77,3 +107,105 @@
77
107
  <% end %>
78
108
 
79
109
  ```
110
+
111
+
112
+
113
+ ```
114
+
115
+ (farmer.rb)
116
+
117
+ class Farmer < ApplicationRecord
118
+
119
+ attr_accessor :remember_token
120
+
121
+ before_save {self.email = email.downcase }
122
+
123
+
124
+
125
+ validates :name, presence: true, length: {maximum: 50}
126
+
127
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
128
+
129
+ validates :email, presence: true,
130
+
131
+ length: {maximum: 255},
132
+
133
+ format: {with: VALID_EMAIL_REGEX},
134
+
135
+ uniqueness: {case_sensitive: false}
136
+
137
+ has_secure_password
138
+
139
+ validates :password, presence: true, length: {minimum: 6}, allow_nil:true
140
+
141
+ validates :tel, presence: true, length: {in: 10..11}
142
+
143
+ validates :postal_code, presence: true
144
+
145
+ validates :prefecture, presence: true
146
+
147
+ validates :address, presence: true
148
+
149
+ validates :comment, presence: true
150
+
151
+
152
+
153
+ has_many :products, dependent: :destroy
154
+
155
+
156
+
157
+ has_many :farmer_images, dependent: :destroy
158
+
159
+ accepts_nested_attributes_for :farmer_images
160
+
161
+
162
+
163
+ def Farmer.digest(string)
164
+
165
+ cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
166
+
167
+ BCrypt::Password.create(string, cost: cost)
168
+
169
+ end
170
+
171
+
172
+
173
+ def Farmer.new_token
174
+
175
+ SecureRandom.urlsafe_base64
176
+
177
+ end
178
+
179
+
180
+
181
+ def remember
182
+
183
+ self.remember_token = Farmer.new_token
184
+
185
+ update_attribute(:remember_digest, Farmer.digest(remember_token))
186
+
187
+ end
188
+
189
+
190
+
191
+ def authenticated?(remember_token)
192
+
193
+ return false if remember_digest.nil?
194
+
195
+ BCrypt::Password.new(remember_digest).is_password?(remember_token)
196
+
197
+ end
198
+
199
+
200
+
201
+ def forget
202
+
203
+ update_attribute(:remember_digest, nil)
204
+
205
+ end
206
+
207
+ end
208
+
209
+
210
+
211
+ ```