Domanda

I have 3 levels of controllers, because I otherwise would have to face a lot of duplication. The 1st level then inherits from InheritedResources::Base in the gem inherited_resources. Somewhere between the controller files I loose the scope.

First the view files:

# 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'

I am getting this error that refers to the delete action on the last line in the table:

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'

Now, take a look at the controllers.

3rd level:

#controllers/hosts/feedback_hosts_controller.rb

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

2nd level:

#controllers/hosts_controller.rb

class HostsController < ScopedElementsController
end

1st level:

#controllers/scoped_elements_controller.rb

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

And finally, the routes:

namespace :hosts do
  resources :feedback_hosts
end
È stato utile?

Soluzione

You need to tweak your call to the link helper a bit. Something like this so it knows to nest the path to the element:

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

Altri suggerimenti

run

rake routes 

in console, i think your path name should be something like "hosts_feedback_hosts_path"

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top