views:

1055

answers:

3

I'm new to rails and can't figure out this issue...

I have a controller

Admin::Blog::EntriesController

defined in app/controllers/admin/blog/entries_controller.rb

And I have a model called

Blog::Entry

defined in app/model/blog/entry.rb

When I try to access my model from the controller, I get a "uninitialized constant Admin::Blog::EntriesController::Blog" from this line:

@blog_entries = Blog::Entry.find(:all)

Clearly it is not finding the namespace correctly which is odd because according to what I have read, I have placed my model in the correct folder with the correct syntax.

Any ideas on how I can fix this?

Thanks

+7  A: 

Try:

@blog_entries = ::Blog::Entry.find(:all)

It's currently looking for the wrong class. Using :: before Blog will force it to look from the top level.

tomafro
Ah, thanks, that did work. However, it turns out that I have to set a custom table name for my model as well. Also, the form_for helper dosn't work as it uses "<model path>_path" thing for the action attribute I think, so it throws an error that it can't find the method "blog_entries_path"... Any ideas?
nlaq
I'm starting to think that rails frowns upon multiple namespaces for models and controllers... Which is a shame because I really like the <section>/<module>/<model> path structure.
nlaq
the rails convention is that model names are singular while controllers are plural. Maybe rename the controller Admins::Blogs::EntriesController (yes, that looks strange)Maybe you should also rethink using namespaced models, they are probably more trouble than they're worth, see http://stackoverflow.com/questions/601768/namespaced-models-in-rails-whats-the-state-of-the-union
levinalex
+1  A: 

You can achieve a custom table name by using

set_table_name('foo')

at the top of your model.

As for multiple namespaces, you might be able to get away with using

polymorphic_path(@the_object)

to generate your urls as it does more basic inference (in my experience at least, maybe form_for uses it under the hood).

Cody Caughlan
A: 

Yeah, from looking at the code form_for uses polymorphic_path under the hood.

Keith Pitt