简体   繁体   English

找不到ListingsController的动作“ destroy”

[英]The action 'destroy' could not be found for ListingsController

UPDATE - I'VE NOW SOLVED THIS PROBLEM - I created a partial for the each Course item and rendered them from the main listing view. 更新-我现在已经解决了这个问题 -我为每个课程项目创建了一个局部,并从主列表视图呈现了它们。 Thanks for all your help, I'm not really sure why it worked but it did END OF UPDATE 感谢您的所有帮助,我不太确定为什么行得通,但是确实行得通

Apologies if this looks like a repeat posting but I've tried applying solutions to similar questions and they haven't worked, I'm stuck! 抱歉,如果这看起来像是重复发帖,但我曾尝试将解决方案应用于类似的问题,但它们没有起作用,我就被困住了! Any suggestions welcomed, thank you. 任何建议都欢迎,谢谢。

Problem I have a 'Courses' model which belongs to a 'Listings' model. 问题我有一个属于“清单”模型的“课程”模型。 The courses are created and deleted on a page belonging to Listing ie "/listing/2/courses" 在属于清单的页面上创建和删除课程,即“ / listing / 2 / courses”

Error Message 错误信息

No route matches [DELETE] "/listings/2/courses" 

Courses Controller def destroy detail 课程控制器定义销毁细节

class CoursesController < ApplicationController

  before_action :authenticate_user!, except: [:show]
  before_action :set_listing
  before_action :set_course, except: [:index, :new, :create]

  def destroy
    @course = @listing.courses.find(params[:id])
    @course.destroy
    flash[:notice] = "Course deleted!"
    redirect_back(fallback_location: request.referer)
  end

  private

  def set_course
    @listing = Listing.find(params[:listing_id])
    @course = Course.find(params[:id])
  end

  def set_listing
    @listing = Listing.find(params[:listing_id])
  end

  def course_params
    params.require(:course).permit(:name, :curriculum_type, :summary, :address, :course_places, :start_date, :finish_date, :price)
  end

end

listing/listingID/courses page detail 清单/清单ID /课程页面详细信息

<%= @listing.courses.each do |f| %>
  <div class="jumbotron">
    <ul>
    <li>Name = <%= f.name %></li>
    <li>Type of course = <%= f.curriculum_type %></li>
    <li>Number of places = <%= f.course_places %></li>
    <li>Start Date = <%= f.start_date %></li>
    <li>Finish Date = <%= f.finish_date %></li>
    <li>Price (£) = <%= f.price %></li>
    <%= link_to "Delete Course", listing_courses_path(@listing, @course), method: :delete %>
    </ul>
  </div>
<% end %>

Routes.rb detail Routes.rb详细信息

resources :users, only: [:show]
  resources :listings, except: [:edit] do
    member do
      get 'listing'
      get 'pricing'
      get 'description'
      get 'photo_upload'
      get 'amenities'
      get 'location'
      get 'courses'
    end
        resources :courses, except: [:edit] do
          member do
            get 'listing'
            get 'pricing'
            get 'description'
            get 'photo_upload'
            get 'amenities'
            get 'location'
        end
    end
  end
<%= link_to listing_course_path(@listing,f), :method => :delete, :data => { :confirm => 'Are you sure?' } %>

or try 或尝试

  <%= link_to listing_course_path(@listing,id: f.try(:id)), :method => :delete, :data => { :confirm => 'Are you sure?' } %>

route.rb route.rb

 resources :listings do
      resources :courses
 end

By default, destroy method expects an ID as it's a member route. 默认情况下, destroy方法需要一个ID,因为它是成员路由。 You are using listing/listingID/courses route without an ID. 您正在使用没有ID的listing/listingID/courses路线。 For that you need to define listing and/or courses as singular resources ( read this ) like: 为此,您需要将listing和/或courses定义为单一资源(请阅读 ),例如:

resource :courses do
:
end

as described in this answer or make destroy a collection route like so: 按照此答案中的说明进行操作,或按照以下步骤destroy collection路线:

resources :courses, except: [:edit] do
  delete :destroy, on: :collection
  :
  rest...
end

Try and see if this works. 尝试看看是否可行。

By the way, this looks a bit redundant as you are iterating over each @listing.courses and calling courses#destroy where you are destroying all the courses of @listing anyhow. 顺便说一句,当您遍历每个@listing.courses并调用@listing courses#destroy ,这看起来有点多余,在这里您无论如何都破坏了@listing所有courses So, why do @listing.courses.each in the first place. 因此,为什么要首先使用@listing.courses.each You should have either used a listing#destroy_courses method or remove @listing.courses.each iteration. 您应该已经使用了listing#destroy_courses方法或删除了@listing.courses.each迭代。

将link_to中的路径从listing_courses_path(@listing,@course)更新为listing_course_path(@listing,@course)

<%= link_to "Delete Course", listing_course_path(@listing, f), method: :delete %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM