Rails RecordNotFound 找不到带有 id 的专辑

Rails RecordNotFound couldnt find album with id

我在嵌套资源 Rails 应用程序的末尾,但我似乎无法弄清楚如何删除评论。我可以删除相册就好了。我的评论属于相册。用户同时拥有相册和评论。

我还希望能够确保用户无法删除不是他们写的评论。我认为这就是我的错误来源。

我不断收到此错误:

ActiveRecord::RecordNotFound in AlbumsController#destroy
Couldn't find Album with 'id'=17
Extracted source (around line #57):
55
56
57
58
59
60


    def set_album
        @album = Album.find(params[:id])
    end

    def album_params

这是我的评论控制器:

class ReviewsController < ApplicationController
    before_action :set_review, only: [:show, :delete, :edit, :update]
    before_action :set_current_user, only: [:index, :new, :edit, :delete]
    before_action :find_album, only: [:create, :edit]
    before_action :must_login, only: [:index, :new, :create, :edit, :update, :delete]

    def index
        @albums = Album.with_recent_reviews
    end

    def show
        #@reviews = Review.where("album_id = ?", params[:album_id])
    end

    def new
        if params[:album_id] && @album = Album.find_by(id: params[:client_id])
            @review = @album.reviews.build
        else
            redirect_to albums_path
        end
    end

    def create
        @review = current_user.reviews.build(review_params)
        @review.album = @album
        if @review.save
            redirect_to album_path(@album)
        else
            @album = @review.album
            render :new
        end
    end

    def edit
    end

    def update
        if @review.update(review_params)
            redirect_to album_path(params[:album_id])
        else
            render 'edit'
        end
    end

    def destroy
        if current_user.id == @review.user_id
            @review.destroy
            redirect_to album_path(params[:album_id])
        else
            flash[:error] = "Unable to delete your review. Please try again."
            redirect_to album_reviews_path(@review)
        #@review.destroy
        end
    end

    private

    def set_review
        @review = Review.find(params[:id])
    end

    def set_current_user
        @user = current_user
    end

    def find_album
        @album = Album.find(params[:album_id])
    end

    def review_params
        params.require(:review).permit(:title, :date, :content, :user_id, :album_id, album_attributes:[:artist, :title, :user_id])
    end

end

这是我的评论索引表,删除 link 所在的位置:

<% @albums.each do |album| %>
  <br>
  <br>
  <% if album.avatar&.attached? %>
    <image src="<%=(url_for(album.avatar))%>%" style="width:350px;height:350px;">
  <% end %>
  <br>
  <%= album.artist %> - <%= album.title %>
  <br>
  <%= link_to "View Album", album_path(album) %>
  <%= link_to "Edit Album", edit_album_path(album) %>
  <br><br>
  <% album.reviews.each do |review| %>
    <% unless review.id.nil? %>
        <small>Date written: <%= review.date %></small><br>
        <small>Written by: <%= review.user.name %></small><br>
        <strong>Title: <%= review.title %></strong><br>
        Review: <%= review.content %><br>
        <%= link_to "Edit Review", edit_album_review_path(album_id: album.id, id: review.id) %>
        <%= link_to 'Delete', album_path(album_id: album.id, id: review.id), method: :delete, data: { confirm: 'Are you sure?' } %>
        <br><br>
    <% end %>
<% end %>
  <br>
<% end %>

相册管理员:

class AlbumsController < ApplicationController
    before_action :set_album, only: [:show, :edit, :update, :destroy]
    before_action :must_login, only: [:new, :show, :create, :edit, :update, :destroy]

    def index
        @albums = Album.all
        @user = current_user
    end

    def show
        @review = @album.reviews.build
        @review.user = current_user
        # If you want to have some flag to indicate its status
        #@review.draft = true
        @review.save
        @reviews = Review.recent #scope
    end

    def new
        @album = Album.new
        @user = current_user
    end

    def create
        @user = User.find(current_user.id)
        @album = current_user.albums.build(album_params)
        @album.user_id = current_user.id
        if @album.save
            redirect_to album_path(@album)
        else
            render :new
        end
    end

    def edit
        @user = current_user
    end

    def update
        #@album = current_user.albums.build(album_params)
        @album.user_id = current_user.id
        if @album.update(album_params)
            redirect_to album_path(@album), notice: "Your album has been updated."
        else
            render 'edit'
        end
    end

    def destroy
        @album.delete
        redirect_to albums_path
    end

    private

    def set_album
        @album = Album.find(params[:id])
    end

    def album_params
        params.require(:album).permit(:artist, :title, :avatar, :user_id, review_attributes:[:title, :date, :content, :user_id, :album_id])
    end
end

您遇到此问题是因为您没有将 album_id 传递给审查控制器的销毁操作。您需要 album_id 的原因是因为您的路线可能看起来像这样(因为您说它们是嵌套的):

http://localhost:3000/albums/1/reviews

您可以在您的路线中看到您需要 album_id(相册后的 1)。尝试将您的评论销毁操作更改为:

def destroy
  if current_user.id == @review.user_id
    @album.reviews.find(params[:id]).destroy
    redirect_to album_path(params[:album_id])
  else
     flash[:error] = "Unable to delete your review. Please try again."
     redirect_to album_reviews_path(@review)
  end
end

您还必须将评论控制器中的 before_action 更改为此,因为您需要 album_id 来销毁评论:

before_action :find_album, only: [:create, :update, :destroy]