4
votes

I am using ActsAsTaggableOn to add tagging to my application. In addition to the features i get from this gem, I would also like to add a TagsController and basically treat tags as any other resource in my app.

I have created tags_controller.rb which contains

class ActsAsTaggableOn::TagsController < ApplicationController
    # ...
end

and in my routes.rb i have added

resources :tags, :module => :acts_as_taggable_on

When I run rake routes i get

    tags GET    /tags(.:format)             {:action=>"index", :controller=>"acts_as_taggable_on/tags"}
         POST   /tags(.:format)             {:action=>"create", :controller=>"acts_as_taggable_on/tags"}
 new_tag GET    /tags/new(.:format)         {:action=>"new", :controller=>"acts_as_taggable_on/tags"}
edit_tag GET    /tags/:id/edit(.:format)    {:action=>"edit", :controller=>"acts_as_taggable_on/tags"}
     tag GET    /tags/:id(.:format)         {:action=>"show", :controller=>"acts_as_taggable_on/tags"}
         PUT    /tags/:id(.:format)         {:action=>"update", :controller=>"acts_as_taggable_on/tags"}
         DELETE /tags/:id(.:format)         {:action=>"destroy", :controller=>"acts_as_taggable_on/tags"}

… which all looks reasonable to me.

However, when I hit localhost:3000/tags i get this error:

LoadError (Expected MyApp/tags_controller.rb to define TagsController)

If I try to evaluate ActsAsTaggableOn::TagsControllerin the console I get basically the same error:

LoadError: Expected MyApp/app/controllers/tags_controller.rb to define TagsController
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:492:in `load_missing_constant'
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing'
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each'
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing'
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:503:in `load_missing_constant'
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:183:in `block in const_missing'
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `each'
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:181:in `const_missing'
    from (irb):1
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start'
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start'
    from ~/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.4/lib/rails/commands.rb:23:in `'
    from script/rails:6:in `require'
    from script/rails:6:in `'

What am I doing wrong?

2

2 Answers

1
votes

Rails expects modules and classes to be in their namespace's directory. So you should move your controller, views and helpers in an acts_as_taggable_on directory:

  • app/controllers/acts_as_taggable_on/tags_controller.rb
  • app/views/acts_as_taggable_on/tags/index.html.erb
  • etc.

I'm not sure why you want this namespace. If you only want to create REST actions on an existing model, you can generate a scaffold_controller. For example:

rails generate scaffold_controller Tag name:string
0
votes

Michaël is right about the directory structure for namespaced controllers.

That being said, a controller does not have to be in the same namespace as a model. On the contrary, you can have a controller named SomeController working with a model named SomeModel. Have you already tried to drop the namespace from the controller class and see what goes wrong?