Ruby model on rails and controllers inside different namespaces
OK. This is madness.
I'm new to RoR and I really want to get into it as everything I've seen so far makes it more appealing for the type of work I do.
However, I cannot accomplish a very simple thing with RoR.
I need these controllers:
/admin/blog/entries (index/show/edit/delete)
/admin/blog/categories (index/show/edit/delete)
/admin/blog/comments (index/show/edit/delete)
... and so on
And these models:
Blog::Entry (table: blog_entries)
Blog::Category (table: blog_categories)
Blog::Comments (table: blog_comments)
... and so on
Now, I've already gone through quite a bit of suffering to make this work. My first attempt was to create forests (I am using 2.2.2). I created my scaffolding, but I had to move my model and then fix the model references in my controller (see Ruby on Rails model inside namespace not found in controller ).
It's already a big pain, but hey, I got it to work. Now though form_for won't work and I can't figure out how to use the URL helpers (I have no idea what they are called ... these are auto-generated methods that return URLs to the model-bound controllers). I can't figure out what their names are. My Model - Blog :: Posts. I tried to use the route.rb resource map method but no luck. When I try to use form_for with my model I get this error
undefined method `blog_entries_path' for #<ActionView::Base:0xb6848080>
Now. This is very unpleasant. I'm not going to completely ruin my code organization to use this infrastructure, and if I can't figure out how to accomplish this simple task (I've researched it for at least 5 hours), then I just can't continue.
Any ideas on how to do this?
thanks
EDIT
Here are my routes:
admin_blog_entries GET /admin_blog_entries {:controller=>"admin_blog_entries", :action=>"index"}
formatted_admin_blog_entries GET /admin_blog_entries.:format {:controller=>"admin_blog_entries", :action=>"index"}
POST /admin_blog_entries {:controller=>"admin_blog_entries", :action=>"create"}
POST /admin_blog_entries.:format {:controller=>"admin_blog_entries", :action=>"create"}
new_admin_blog_entry GET /admin_blog_entries/new {:controller=>"admin_blog_entries", :action=>"new"}
formatted_new_admin_blog_entry GET /admin_blog_entries/new.:format {:controller=>"admin_blog_entries", :action=>"new"}
edit_admin_blog_entry GET /admin_blog_entries/:id/edit {:controller=>"admin_blog_entries", :action=>"edit"}
formatted_edit_admin_blog_entry GET /admin_blog_entries/:id/edit.:format {:controller=>"admin_blog_entries", :action=>"edit"}
admin_blog_entry GET /admin_blog_entries/:id {:controller=>"admin_blog_entries", :action=>"show"}
formatted_admin_blog_entry GET /admin_blog_entries/:id.:format {:controller=>"admin_blog_entries", :action=>"show"}
PUT /admin_blog_entries/:id {:controller=>"admin_blog_entries", :action=>"update"}
PUT /admin_blog_entries/:id.:format {:controller=>"admin_blog_entries", :action=>"update"}
DELETE /admin_blog_entries/:id {:controller=>"admin_blog_entries", :action=>"destroy"}
DELETE /admin_blog_entries/:id.:format {:controller=>"admin_blog_entries", :action=>"destroy"}
home / {:action=>"index", :controller=>"index"}
/:controller/:action/:id
/:controller/:action/:id.:format
It doesn't look right. Here are my .rb routes (comments removed):
ActionController::Routing::Routes.draw do |map|
map.resources :admin_blog_entries
map.home '', :controller => 'index'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
a source to share
Have you tried looking at the route list that "rake routes" gives you? if your route.rb is correct it should show you the correct name for the route of the blog entries.
maybe this can help: http://www.coreywoodcox.com/2008/08/18/rails-namespaces-subdomains/ .
change:
well, then the correct way to call the route is admin_blog_entries_path, not blog_entries_path.
a source to share
Your .rb routes should look like this:
map.namespace :admin do |admin|
admin.namespace :blog do |blog|
blog.resources :entries
blog.resources :categories
...
end
end
but I'm not sure how to handle that "/ blog /" part in your url (I haven't used the namespace in my models yet). But with these routes, you should be able to use:
admin_blog_categories_path => '/admin/blog/categiries'
admin_blog_category_path(@some_category) => '/admin/blog/categories/1'
etc.
a source to share
Ok, here's my rather hacky way of doing it, which I don't like, but works.
In my case, I have the Blog :: Article, Blog :: Comment models nested in routes. One warning if you are using this approach is in Blog :: CommentsController when loading an article, you can expect params [: article_id] or params [: blog_article_id]. Not pleasant in any way, but as I said. He works:/
blog.resources :articles do |article|
article.resources :comments
end
blog.resources :blog_articles, :controller => 'articles' do |blog_article|
blog_article.resources :blog_comments, :controller => 'comments'
end
a source to share