ruby-on-rails 删除方法Rails -无路径匹配[GET]“/bookmarks/11”

rdrgkggo  于 2023-01-18  发布在  Ruby
关注(0)|答案(1)|浏览(142)

我的书签删除方法似乎不起作用,这是rails 7的问题还是我做错了什么。它声明没有Get方法。我使用的是turbo-pack方法而不是webpack方法
以下是所有相关文件
routes.rb

Rails.application.routes.draw do
  root 'lists#index'
  resources :lists, except: [:edit, :update] do
    resources :bookmarks, only: [:new, :create]
  end

  resources :bookmarks, only: :destroy
end

书签_控制器.rb

class BookmarksController < ApplicationController
  before_action :set_bookmark, only: :destroy
  before_action :set_list, only: [:new, :create]

  def new
    @bookmark = Bookmark.new
  end

  def create
    @bookmark = Bookmark.new(bookmark_params)
    @bookmark.list = @list
    if @bookmark.save
      redirect_to list_path(@list)
    else
      render :new
    end
  end

  def destroy
    @bookmark.destroy
    redirect_to list_path(@bookmark.list), status: :see_other
  end

  private

  def bookmark_params
    params.require(:bookmark).permit(:comment, :movie_id)
  end

  def set_bookmark
    @bookmark = Bookmark.find(params[:id])
  end

  def set_list
    @list = List.find(params[:list_id])
  end
end

show.html.erb

<div class="row">
<% @list.movies.each do |movie|%>
 <% bookmark = Bookmark.find_by(list: @list, movie: movie)  %>
  <div class="col-12 col-lg-4">
      <div class="card text-center text-white" style="background-color:#ffa82e00;">
        <div class="card photo" class="card-img-top">
          <%= image_tag(movie.poster_url, class: "card-img-top" )%>
          <div class="card-body">
            <h5 class="card-title text-dark"><%= movie.title%></h5>
            <p class="card-text text-body"><%= movie.overview %></p>
            <p class="card-text text-body"><%= bookmark.comment%></p>
            <%= link_to "delete", bookmark_path(bookmark), data: { turbo_method: :delete, turbo_confirm: "Are you sure you want to remove #{movie.title} from your #{@list.name} list"}, class: 'text-danger' %>
          </div>
        </div>
      </div>
  </div>
<% end %>
<%= link_to "Bookmark now", new_list_bookmark_path(@list), class: "btn btn-primary" %>
oyjwcjzk

oyjwcjzk1#

我认为这是因为你使用的link_to会发送一个GET请求。尝试使用button_for代替。

相关问题