行动“摧毁”#39;找不到ListingsController

时间:2017-10-25 11:19:40

标签: ruby-on-rails delete-method

更新 - 我现在已经解决了这个问题 - 我为每个课程项目创建了一个部分,并从主列表视图中呈现它们。感谢您的帮助,我不确定为什么会有效,但确实 END OF UPDATE

道歉,如果这看起来像重复发布,但我已尝试将解决方案应用于类似的问题而且他们还没有工作,我就被卡住了!欢迎任何建议,谢谢。

问题 我有一个'课程'属于'列表的模型'模型。课程在属于列表的页面上创建和删除,即" / listing / 2 / courses"

错误消息

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

课程控制器def销毁细节

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

列出/列出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详情

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

3 个答案:

答案 0 :(得分:1)

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

或尝试

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

route.rb

 resources :listings do
      resources :courses
 end

答案 1 :(得分:0)

默认情况下,destroy方法需要一个ID,因为它是成员路由。您正在使用没有ID的listing/listingID/courses路由。为此,您需要将listing和/或courses定义为单数资源(read this),例如:

resource :courses do
:
end

如此answer中所述,或destroy collection路线如此:

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

试试看是否有效。

顺便说一句,这看起来有点多余,因为你在每个@listing.courses上进行迭代并调用courses#destroy,无论如何你正在摧毁courses的所有@listing。那么,为什么首先@listing.courses.each呢?您应该使用listing#destroy_courses方法或删除@listing.courses.each次迭代。

答案 2 :(得分:0)

从listing_courses_path(@listing,@course)更新link_to到listing_course_path(@listing,@course)的路径

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