Ruby on Rails:销毁操作显示错误

时间:2017-12-09 10:52:51

标签: ruby ruby-on-rails-5

如果执行destroy操作,则会出现这样的错误。 网址:http://localhost:3000/listings/test

enter image description here

查看

<% @alllistings.all.order(created_at: :desc).each do |listing| %>
        <tbody>
          <tr>
            <th scope="row">
              <%= link_to listing_path(listing) do %>
                <span class="listing-title"><%= listing.listing_title %></span>
              <% end %>
            </th>
            <td>
              <%= link_to "delete",[listing],method: :delete, data: {confirm: "Are you sure?"} if current_user = listing.user %>
            </td>
          </tr>
        </tbody>
  <% end %>

控制器

class ListingsController < ApplicationController
before_action :set_listing, only: [:destroy]
  def index
    @alllistings = current_user.listings
  end
  def destroy
    @listing.destroy

    return reedirect_to listings_path, notice: "削除しました", :status => :moved_permanently
  end

private
  def set_listing
    @listing = Listing.friendly.find(params[:id])
  end
end

模型

class Listing < ApplicationRecord
 validates :listing_title, presence: true

  include FriendlyId
  friendly_id :listing_title

  def should_generate_new_friendly_id?
     listing_title_changed?
  end
end

铁轨5 我们正在使用gem'friendly_id'。 我们还介绍了slug。

请回答我的问题。

1 个答案:

答案 0 :(得分:0)

请尝试在您的视图中使用。

<%= link_to "delete",listing_path(listing), method: :delete, data: {confirm: "Are you sure?"} if current_user = listing.user %>