0
destroyアクションを実行すると、このようなエラーが発生します。 URL:http://localhost:3000/listings/testRuby on Rails:Destroyアクションでエラーが表示される
ビュー
<% @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'を使用しています。 また、スラグも紹介しました。
私の質問にお答えください。
他のモデルとの関連付けをしてください。 – Imran
コンソールから完全なエラーをコピーして貼り付けることはできますか。 – Imran
外部キー制約エラーが発生します。ユーザーモデルファイルとリストおよびユーザーの移行ファイルを追加してください。 –