Namespaced controller causes error in Rails-3 - ruby-on-rails-3

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?

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

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?

Related

rspec2 routing error for index page [Rails3, Rspec2]

I have the following resource (config/routes.rb)
resources :dashboard
dashboard_index GET /dashboard(.:format) dashboard#index
POST /dashboard(.:format) dashboard#create
new_dashboard GET /dashboard/new(.:format) dashboard#new
edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit
dashboard GET /dashboard/:id(.:format) dashboard#show
PUT /dashboard/:id(.:format) dashboard#update
DELETE /dashboard/:id(.:format) dashboard#destroy
and I have the following controller spec
require 'spec_helper'
describe DashboardController do
describe "GET 'index'" do
it "returns http success" do
get :index
response.should be_success
end
end
end
and when I run the spec I'm keep on getting the following error
Failures:
1) DashboardController GET 'index' returns http success
Failure/Error: get :index
ActionController::RoutingError:
No route matches {:controller=>"dashboard"}
# ./spec/controllers/dashboard_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.12126 seconds
when I check for similar questions, almost all of them are because not passing required parameters, but in my case my action does not expect a parameter, so what would be the missing part here
I'm on
gem 'rails', '3.2.9'
group :test, :development do
gem "rspec-rails", "~> 2.0"
end
thanks in advance
as #John Naegle pointed out, it appears to be a problem with my spork + guard setup and its not picking up the 'routes.rb' file
Sorry for the confusion

ThinkingSphinx Error: such filter attribute

I am trying to integrate ThinkingSphinx into my project using the doc. For a User model with attributes first_name and last_name I have created the indexes as:
User.rb
define_index do
indexes :first_name
indexes :last_name
end
Then I run the commands:
rake ts:index
rake ts:start
This starts my search. I get the results when I use the following command to search:
User.search('swati')
But When I run
User.search :with => {:first_name => "swati"}
It gives me the error:
Sphinx Sphinx Daemon returned error: index user_core: no such filter attribute 'first_name'
Sphinx Caught Sphinx exception: index user_core: no such filter attribute 'first_name' (0 tries left)
ThinkingSphinx::SphinxError: index user_core: no such filter attribute 'first_name'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:438:in `block in populate'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:606:in `call'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:606:in `retry_on_stale_index'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:426:in `populate'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/thinking-sphinx-2.0.13/lib/thinking_sphinx/search.rb:187:in `method_missing'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /home/swati/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I searched for a possible solution on stackoverflow and it has been asked to run the commands rake ts:index and rake ts:rebuild. I have run this commands but it didn't help me.
Please help me figure out a solution for this.
Many thanks.
You should create an attribute with has in the index, rebuild, and then use with
has :first_name
Has creates an attribute, and with is used as a filter (for attributes) http://pat.github.com/ts/en/searching.html#filters
Also check http://pat.github.com/ts/en/indexing.html#attributes for filters.
Be carefull, rake ts:reindex and rake ts:rebuild are different.
If you add a new index, you need to rebuild.
If you are paranoiac like me, try everything:
rake ts:rebuild
rake ts:reindex
rake ts:restart
If it still doesn't work, make sure your index is defined properly.

Undefined method for a belongs_to association

My code:
class User < ActiveRecord::Base
belongs_to :university
end
class University < ActiveRecord::Base
has_many :users, dependent: :destroy
end
and my model User has a university_id attribute.
If I do University.find(1).users I get the list of users, but if I do User.find(1).university (and I checked that university_id is not nil here) I get:
NoMethodError: undefined method `university' for #<User:0x00000003859fc8>
from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.0.10/lib/active_model/attribute_methods.rb :392:in `method_missing'
from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/attribute_methods. rb:46:in `method_missing'
from (irb):14
from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in`start'
from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in start'
from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands.rb:23:in
`<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
What am I doing wrong? I have another models and they are working just fine. Any suggestions? Thanks in advance
I still can't comment so I'll burn an answer:
Somehow the belongs_to :university in the User model isn't being recognized. When testing, are you certain that the User model has been saved and is in the right place and that the server or console has been refreshed? Most commonly, in my experience, when I'm meddling with models, I have to refresh my server and console often to get clean results.
Try
User.where("id =?", 1).first.university

"No route matches" error?

I am new Rspec and just started by generating a new controller on Rails 3. It generates some Rspec tests by default. I have a question about how to make them pass though. As it stands, I see this test in my terminal"
1) BuildingsController GET 'show'
should be successful
Failure/Error: get 'show'
No route matches {:controller=>"buildings", :action=>"show"}
# ./spec/controllers/buildings_controller_spec.rb:17:in `block (3 levels) in <top (required)>'
However, I don't understand why it's coming up because I already have this route created ("resources :buildings"), and I ran rake routes and made sure it's there.
building GET /buildings/:id(.:format) {:action=>"show", :controller=>"buildings"}
What is necessary to make this pass? Here is the test by the way:
describe "GET 'show'" do
it "should be successful" do
get 'show'
response.should be_success
end
end
You need to pass id of existing building: get :show, :id => #building.id
Routes complain about it because :id isn't optional.

MongoMapper won't let me create an object

I'm just learning MongoDB and MongoMapper. This is on Rails 3.
I created a blog in app/models/blog.rb:
class Blog
include MongoMapper::Document
key :title, String, :required => true
key :body, Text
timestamps!
end
I go into the Rails console:
rails c
Loading development environment (Rails 3.0.0.beta)
ruby-1.9.1-p378 > b = Blog.new
NoMethodError: undefined method `from_mongo' for Text:Module
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:323:in `get'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:269:in `read_key'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/keys.rb:224:in `[]'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:7:in `block in inspect'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:6:in `collect'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/mongo_mapper-0.7.2/lib/mongo_mapper/plugins/inspect.rb:6:in `inspect'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:47:in `start'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands/console.rb:8:in `start'
from /Users/jade/.rvm/gems/ruby-1.9.1-p378/gems/railties-3.0.0.beta/lib/rails/commands.rb:34:in `<top (required)>'
from /Users/jade/code/farmerjade/script/rails:10:in `require'
from /Users/jade/code/farmerjade/script/rails:10:in `<main>'
Am I overlooking something really dumb, or is this something in my setup?
I'm using the mongo_mapper version you get by adding it to your Gemfile, so I'm wondering if it might be that. I'd appreciate any suggestions!
As I suspected, this was a silly mistake. I was using Text instead of String, and Mongo doesn't natively support Text as a key. If you define a from_mongo method, it would.
Thanks to DanP on IRC for pointing out my error.