閲覧いただきまして、ありがとうございます。
Ruby on Rails 5.0.0.1で口コミサイトを開発しています。
ユーザーに紐付いた口コミが更新されずに困っています。
「パラメーターがない!」とエラーで指摘を受けているのですが、実際、createしているときと同じものをいれているので間違いないはずなのですが・・・
Ruby
1app[web.1]: Completed 500 Internal Server Error in 7ms (ActiveRecord: 1.7ms) 2app[web.1]: ActionController::UnpermittedParameters (found unpermitted parameter: id): 3app[web.1]: app/controllers/reviews_controller.rb:54:in `create_params' 4app[web.1]: app/controllers/reviews_controller.rb:20:in `update'
##ソースコード
reviewモデル
Ruby
1class Review < ApplicationRecord 2 belongs_to :user 3 belongs_to :product 4 has_one :review_image 5 accepts_nested_attributes_for :review_image 6 7 default_scope -> { order(created_at: :desc) } 8 validates :user_id, presence: true 9 validates :product_id, presence: true 10 validates :content, presence: true, length: { minimum: 200 } 11end
すみませんが、どうぞ宜しくお願いします。
##追記
Ruby
1 <div class="review-edit"> 2 <%= form_for(@review) do |f| %> 3 <div class="post-rate"> 4 <div class="form-group"> 5 <label for="rate">評価</label> 6 <%= f.select :rate, {'1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5}, class: "form-control" %> 7 </div> 8 </div> 9 <div class="post-contents"> 10 <div class="form-group"> 11 <label for="textarea">口コミを入力</label> 12 <%= f.text_area :content, class: "form-control", placeholder: "口コミ内容", rows: "10", onKeyUp: "countLength(value, 'textlength')" %> 13 <p>現在<span id="textlength">0文字</span></p> 14 </div> 15 </div> 16 <div class="post-photo"> 17 <div class="form-group"> 18 <label for="review_image">画像を投稿</label> 19 <%= f.fields_for :review_image do |review_image| %> 20 <%= review_image.file_field :image %> 21 <% end %> 22 </div> 23 </div> 24 <%= f.submit "更新する", class: "btn" %> 25 <% end %> 26 </div>
##追記エラー
heroku[router]: at=info method=POST path="/reviews/114" dyno=web.1 connect=1ms service=32ms status=500 bytes=1827 protocol=http app[web.1]: Started PATCH "/reviews/114" for 108.162.246.164 at 2017-08-15 08:12:08 +0000 app[web.1]: Processing by ReviewsController#update as HTML app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"R+GNVKUKE/9SPW4ULsVptV7C6bVw==", "review"=>{"rate"=>"5", "content"=>"口コミが入ります。", "review_image_attributes"=>{"id"=>"20"}}, "commit"=>"編集する", "id"=>"114"} 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]] app[web.1]: Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.9ms) app[web.1]: ActionController::UnpermittedParameters (found unpermitted parameter: id): app[web.1]: app/controllers/reviews_controller.rb:55:in `create_params' app[web.1]: app/controllers/reviews_controller.rb:33:in `update'
reviewコントローラー
class ReviewsController < ApplicationController before_action :set_review, only:[:show, :edit, :update, :destroy] before_action :correct_user, only: [:edit, :destroy] def create @review = current_user.reviews.create(create_params) if @review.save flash[:success] = "口コミが投稿されました!" redirect_to root_path else flash[:alert] = "口コミの投稿に失敗しました。" redirect_to root_path end end def new @product = Peroduct.find(params[:id]) if signed_in? @review = current_user.reviews.build @review.build_review_image else redirect_to new_user_registration_path end end def show end def edit end def update if @review.update_attributes(create_params) flash[:success] = "口コミが更新されました" redirect_to @review else flash[:alert] = "口コミの編集に失敗しました。" render 'edit' end end def destroy @review.destroy flash[:success] = "口コミが削除されました" redirect_to request.referrer || root_url end private def set_review @review = Review.find(params[:id]) end def create_params params.require(:review).permit(:id, :content, :rate, :title, :product_id, :user_id, review_image_attributes: [:image]) end def correct_user @review = current_user.reviews.find_by(id: params[:id]) redirect_to root_url if @review.nil? end end
回答1件
あなたの回答
tips
プレビュー