2017-12-09 14 views
0

destroyアクションを実行すると、このようなエラーが発生します。 URL:http://localhost:3000/listings/testRuby on Rails:Destroyアクションでエラーが表示される

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'を使用しています。 また、スラグも紹介しました。

私の質問にお答えください。

+0

他のモデルとの関連付けをしてください。 – Imran

+0

コンソールから完全なエラーをコピーして貼り付けることはできますか。 – Imran

+0

外部キー制約エラーが発生します。ユーザーモデルファイルとリストおよびユーザーの移行ファイルを追加してください。 –

答えて

0

あなたの目で見てみてください。

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

私はそれを試してみますが、私は削除することはできません! –

関連する問題