質問編集履歴

2

コントローラーおよび、エラーメッセージを追記しました

2017/08/15 08:19

投稿

yamady
yamady

スコア176

title CHANGED
File without changes
body CHANGED
@@ -13,9 +13,70 @@
13
13
 
14
14
  ##ソースコード
15
15
 
16
+ > reviewモデル
17
+
18
+ ```Ruby
19
+ class Review < ApplicationRecord
20
+ belongs_to :user
21
+ belongs_to :product
22
+ has_one :review_image
23
+ accepts_nested_attributes_for :review_image
24
+
25
+ default_scope -> { order(created_at: :desc) }
26
+ validates :user_id, presence: true
27
+ validates :product_id, presence: true
28
+ validates :content, presence: true, length: { minimum: 200 }
29
+ end
30
+ ```
31
+ すみませんが、どうぞ宜しくお願いします。
32
+
33
+ ##追記
34
+
35
+ ```Ruby
36
+ <div class="review-edit">
37
+ <%= form_for(@review) do |f| %>
38
+ <div class="post-rate">
39
+ <div class="form-group">
40
+ <label for="rate">評価</label>
41
+ <%= f.select :rate, {'1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5}, class: "form-control" %>
42
+ </div>
43
+ </div>
44
+ <div class="post-contents">
45
+ <div class="form-group">
46
+ <label for="textarea">口コミを入力</label>
47
+ <%= f.text_area :content, class: "form-control", placeholder: "口コミ内容", rows: "10", onKeyUp: "countLength(value, 'textlength')" %>
48
+ <p>現在<span id="textlength">0文字</span></p>
49
+ </div>
50
+ </div>
51
+ <div class="post-photo">
52
+ <div class="form-group">
53
+ <label for="review_image">画像を投稿</label>
54
+ <%= f.fields_for :review_image do |review_image| %>
55
+ <%= review_image.file_field :image %>
56
+ <% end %>
57
+ </div>
58
+ </div>
59
+ <%= f.submit "更新する", class: "btn" %>
60
+ <% end %>
61
+ </div>
62
+ ```
63
+
64
+ ##追記エラー
65
+ ```
66
+ heroku[router]: at=info method=POST path="/reviews/114" dyno=web.1 connect=1ms service=32ms status=500 bytes=1827 protocol=http
67
+ app[web.1]: Started PATCH "/reviews/114" for 108.162.246.164 at 2017-08-15 08:12:08 +0000
68
+ app[web.1]: Processing by ReviewsController#update as HTML
69
+ app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"R+GNVKUKE/9SPW4ULsVptV7C6bVw==", "review"=>{"rate"=>"5", "content"=>"口コミが入ります。", "review_image_attributes"=>{"id"=>"20"}}, "commit"=>"編集する", "id"=>"114"}
70
+ app[web.1]: Review Load (1.9ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 ORDER BY "reviews"."created_at" DESC LIMIT $2 [["id", 114], ["LIMIT", 1]]
71
+ app[web.1]: Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.9ms)
72
+ app[web.1]: ActionController::UnpermittedParameters (found unpermitted parameter: id):
73
+ app[web.1]: app/controllers/reviews_controller.rb:55:in `create_params'
74
+ app[web.1]: app/controllers/reviews_controller.rb:33:in `update'
75
+ ```
76
+
16
77
  > reviewコントローラー
17
78
 
18
- ```Ruby
79
+ ```
19
80
  class ReviewsController < ApplicationController
20
81
  before_action :set_review, only:[:show, :edit, :update, :destroy]
21
82
  before_action :correct_user, only: [:edit, :destroy]
@@ -32,15 +93,18 @@
32
93
  end
33
94
 
34
95
  def new
35
- @product = Product.find(params[:id])
96
+ @product = Peroduct.find(params[:id])
36
97
  if signed_in?
37
- @post = current_user.reviews.build
98
+ @review = current_user.reviews.build
38
- @post.build_review_image
99
+ @review.build_review_image
39
100
  else
40
101
  redirect_to new_user_registration_path
41
102
  end
42
103
  end
43
104
 
105
+ def show
106
+ end
107
+
44
108
  def edit
45
109
  end
46
110
 
@@ -49,10 +113,17 @@
49
113
  flash[:success] = "口コミが更新されました"
50
114
  redirect_to @review
51
115
  else
116
+ flash[:alert] = "口コミの編集に失敗しました。"
52
117
  render 'edit'
53
118
  end
54
119
  end
55
120
 
121
+ def destroy
122
+ @review.destroy
123
+ flash[:success] = "口コミが削除されました"
124
+ redirect_to request.referrer || root_url
125
+ end
126
+
56
127
  private
57
128
 
58
129
  def set_review
@@ -68,52 +139,4 @@
68
139
  redirect_to root_url if @review.nil?
69
140
  end
70
141
  end
71
- ```
72
-
73
- > reviewモデル
74
-
75
- ```Ruby
76
- class Review < ApplicationRecord
77
- belongs_to :user
78
- belongs_to :product
79
- has_one :review_image
80
- accepts_nested_attributes_for :review_image
81
-
82
- default_scope -> { order(created_at: :desc) }
83
- validates :user_id, presence: true
84
- validates :product_id, presence: true
85
- validates :content, presence: true, length: { minimum: 200 }
86
- end
87
- ```
88
- すみませんが、どうぞ宜しくお願いします。
89
-
90
- ##追記
91
-
92
- ```Ruby
93
- <div class="review-edit">
94
- <%= form_for(@review) do |f| %>
95
- <div class="post-rate">
96
- <div class="form-group">
97
- <label for="rate">評価</label>
98
- <%= f.select :rate, {'1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5}, class: "form-control" %>
99
- </div>
100
- </div>
101
- <div class="post-contents">
102
- <div class="form-group">
103
- <label for="textarea">口コミを入力</label>
104
- <%= f.text_area :content, class: "form-control", placeholder: "口コミ内容", rows: "10", onKeyUp: "countLength(value, 'textlength')" %>
105
- <p>現在<span id="textlength">0文字</span></p>
106
- </div>
107
- </div>
108
- <div class="post-photo">
109
- <div class="form-group">
110
- <label for="review_image">画像を投稿</label>
111
- <%= f.fields_for :review_image do |review_image| %>
112
- <%= review_image.file_field :image %>
113
- <% end %>
114
- </div>
115
- </div>
116
- <%= f.submit "更新する", class: "btn" %>
117
- <% end %>
118
- </div>
119
142
  ```

1

ビューを追記しました

2017/08/15 08:19

投稿

yamady
yamady

スコア176

title CHANGED
File without changes
body CHANGED
@@ -85,4 +85,35 @@
85
85
  validates :content, presence: true, length: { minimum: 200 }
86
86
  end
87
87
  ```
88
- すみませんが、どうぞ宜しくお願いします。
88
+ すみませんが、どうぞ宜しくお願いします。
89
+
90
+ ##追記
91
+
92
+ ```Ruby
93
+ <div class="review-edit">
94
+ <%= form_for(@review) do |f| %>
95
+ <div class="post-rate">
96
+ <div class="form-group">
97
+ <label for="rate">評価</label>
98
+ <%= f.select :rate, {'1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5}, class: "form-control" %>
99
+ </div>
100
+ </div>
101
+ <div class="post-contents">
102
+ <div class="form-group">
103
+ <label for="textarea">口コミを入力</label>
104
+ <%= f.text_area :content, class: "form-control", placeholder: "口コミ内容", rows: "10", onKeyUp: "countLength(value, 'textlength')" %>
105
+ <p>現在<span id="textlength">0文字</span></p>
106
+ </div>
107
+ </div>
108
+ <div class="post-photo">
109
+ <div class="form-group">
110
+ <label for="review_image">画像を投稿</label>
111
+ <%= f.fields_for :review_image do |review_image| %>
112
+ <%= review_image.file_field :image %>
113
+ <% end %>
114
+ </div>
115
+ </div>
116
+ <%= f.submit "更新する", class: "btn" %>
117
+ <% end %>
118
+ </div>
119
+ ```