前提・実現したいこと
編集画面へのリンクを作成したのですが、リンク先が表示されない。
発生している問題・エラーメッセージ
ActiveRecord::RecordNotFound in ReviewsController#edit Couldn't find Review with 'id'=6
該当のソースコード
#routes.rb Rails.application.routes.draw do devise_for :users resources :wines do resources :reviews, except: [:show, :index] end # ログアウト devise_scope :user do get 'sign_out', to: "sessions#destroy" end resources :users, controllers: { registrations: 'users/registrations' } root 'wines#index' end
#show.html.erb . .(略) . <%= link_to "編集する", edit_wine_review_path(review.id), class: "btn btn-light" %> . .(略) .
#reviews_controller.rb class ReviewsController < ApplicationController before_action :set_review, only: [:show, :edit, :update, :destroy] before_action :authenticate_user! before_action :set_wine def new @review = Review.new end def edit end def create @review = Review.new(review_params) @review.user_id = current_user.id @review.wine_id = @wine.id respond_to do |format| if @review.save format.html { redirect_to @wine, notice: 'Review was successfully created.' } format.json { render :show, status: :created, location: @review } else format.html { render :new } format.json { render json: @review.errors, status: :unprocessable_entity } end end end def update respond_to do |format| @review.user_id = current_user.id @review.wine_id = @wine.id if @review.update(review_params) format.html { redirect_to @review, notice: 'Review was successfully updated.' } format.json { render :show, status: :ok, location: @review } else format.html { render :edit } format.json { render json: @review.errors, status: :unprocessable_entity } end end end def destroy @review.destroy respond_to do |format| format.html { redirect_to reviews_url, notice: 'Review was successfully destroyed.' } format.json { head :no_content } end end private def set_review @review = Review.find(params[:id]) end def set_wine @wine = Wine.find(params[:wine_id]) end def review_params params.require(:review).permit(:rating, :comment, :price) end end
rails routes | grep review
の結果を見てみたいです。
パスに必要な変数が用意されていないと考えているため。
windowsなのでrails routes | findstr reviewしてみました。
下記です。
wine_reviews POST /wines/:wine_id/reviews(.:format) reviews#create
new_wine_review GET /wines/:wine_id/reviews/new(.:format) reviews#new
edit_wine_review GET /wines/:wine_id/reviews/:id/edit(.:format) reviews#edit
wine_review PATCH /wines/:wine_id/reviews/:id(.:format) reviews#update
PUT /wines/:wine_id/reviews/:id(.:format) reviews#update
DELETE /wines/:wine_id/reviews/:id(.:format) reviews#destro
回答1件
あなたの回答
tips
プレビュー