类范围在3级深度控制器(轨道)之间丢失

时间:2013-09-11 20:39:59

标签: ruby-on-rails routes controllers nested-routes

我有3级控制器,因为否则我将不得不面对很多重复。然后,第一级继承自gem InheritedResources::Base中的inherited_resources。在控制器文件之间的某处我松开了范围。

首先是视图文件:

# views/hosts/index.html.haml

= render 'hosts_table'

# views/hosts/_hosts_table.html.haml

%table.table
  %thead
    %tr
      %th
        Delete
  %tbody.small-body
    - @elements.each do |element|
      - puts element.inspect # => #<FeedbackHost _id: 522ebcdf83c336b7d6000002, created_at: 2013-09-10 06:31:59 UTC, updated_at: 2013-09-11 11:17:44 UTC, _type: "FeedbackHost">
      %tr
        %td
          = link_to "Delete", element, method: :delete, :remote => true, :class => 'btn btn-small'

我收到此错误,指的是表格中最后一行的删除操作:

Started GET "/hosts/feedback_hosts" for 127.0.0.1 at 2013-09-11 22:20:30 +0200
Processing by Hosts::FeedbackHostsController#index as HTML
  MOPED: 127.0.0.1:27017 QUERY        database=feedbackzilla_development collection=users selector={"$query"=>{"_id"=>"522887dc83c3368361000964"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.7617ms)
  MOPED: 127.0.0.1:27017 QUERY        database=feedbackzilla_development collection=hosts selector={"$query"=>{"user_id"=>"522887dc83c3368361000964", "_type"=>{"$in"=>["FeedbackHost"]}}, "$orderby"=>{"_id"=>1}} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.6032ms)
  Rendered hosts/_hosts_table.html.haml (79.7ms)
  Rendered hosts/index.html.haml within layouts/application (81.9ms)
Completed 500 Internal Server Error in 106ms

NoMethodError - undefined method `feedback_host_path' for #<#<Class:0x007fe3fb3b43e0>:0x007fe3fb4579f0>:
  actionpack (3.2.12) lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url'
  actionpack (3.2.12) lib/action_dispatch/routing/polymorphic_routes.rb:135:in `polymorphic_path'
  actionpack (3.2.12) lib/action_view/helpers/url_helper.rb:111:in `url_for'
  actionpack (3.2.12) lib/action_view/helpers/url_helper.rb:242:in `link_to'
  app/views/hosts/_hosts_table.html.haml:27:in `block in _app_views_hosts__hosts_table_html_haml___3804643964694924097_70308574123920'

现在,看看控制器。

第3级:

#controllers/hosts/feedback_hosts_controller.rb

class Hosts::FeedbackHostsController < HostsController
  def scoped_elements
    "feedback_hosts"
  end
end

第二级:

#controllers/hosts_controller.rb

class HostsController < ScopedElementsController
end

第一级:

#controllers/scoped_elements_controller.rb

class ScopedElementsController < InheritedResources::Base
  def index
    @elements = current_user.send(scoped_elements)
  end
end

最后,路线:

namespace :hosts do
  resources :feedback_hosts
end

2 个答案:

答案 0 :(得分:1)

运行

rake routes 

在控制台中,我认为你的路径名应该是“hosts_feedback_hosts_path”

答案 1 :(得分:1)

您需要稍微调整对链接助手的调用。这样的东西,所以它知道将路径嵌套到元素:

= link_to "Delete", [:hosts, element], method: :delete, :remote => true, :class => 'btn btn-small'