我有 3 个级别的控制器,否则我将不得不面对很多重复。然后第一级继承自InheritedResources::Base
gem 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'
现在,看看控制器。
第三级:
#controllers/hosts/feedback_hosts_controller.rb
class Hosts::FeedbackHostsController < HostsController
def scoped_elements
"feedback_hosts"
end
end
2级:
#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