Hello Rails Talk people
(I am using rails 2.3.5, with ruby 1.8.7)
When I
ruby script/generate scaffold admin/category name:string
or
ruby script/generate scaffold Admin::category name:string
and then
rake db:migrate:up VERSION=20091206005933 whatever...
and configure in routes.rb
map.namespace :admin do |admin|
admin.resources :categories
end
then, when going to
http://www.domain.com/appname/admin/categories
then following error is produced:
Mysql::Error: Table 'sr22x3_development.categories' doesn't exist:
SELECT * FROM `categories`
Now, of course this error is produced, because, after all, there is
indeed NO table
named "categories". That's because the database that the migrations
produced
is named
admin_categories,
not
categories.
Unless I go through a massive renaming of components, such as
suggested by
http://icebergist.com/posts/restful-admin-namespaced-controller-using-scaffolding
then nothing works. (Slobodan's fix, from his site, is given below.)
I thought that subdirectory and Namespace routing was supposed to be
working.
Regards, all
Maurice Yarrow
Slobodan's fix, from his site, is quoted below:
Here is the easiest way I found so far to accomplish this. This
example generates categories model
and controllers for it.
./script/generate controller admin/categories
./script/generate scaffold category name:string
This will generate an empty controller in admin namespace and a
scaffolded resource for front-end
controller.
Now we have everything generated we just need to make it work with
admin controller and not with
front-end.
move all templates from app/views/categories to app/views/admin/
categories
copy all functions from categories_controller.rb to admin/
categories_controller.rb
add namespace for admin controller in routes.rb:
map.namespace :admin do |admin|
admin.resources :categories
end
In admin/categories_controller.rb replace in 3 places redirect_to
calls to work with admin namespace.
It will have something like redirect_to(@category), but to work with
namespace it needs to have
redirect_to([:admin, @category])
make similar changes in all templates, i.e. make it work within an
admin namespace. You need to
make following changes:
form_for(@category) => form_for([:admin, @category])
<%= link_to ‘Show’, @category %> => <%= link_to ‘Show’, [:admin,
@category] %>
categories_path => admin_categories_path
edit_category_path(@category) => edit_admin_category_path(@category)
new_category_path => new_admin_category_path
That’s it. Now you’ll have /admin/categories for all administrative
tasks and you have a free controller
for front-end actions.
You might wonder why not just generate scaffold for admin/categories…
The reason is that you’ll also
get a model that is namespaced in admin (i.e. model would be
Admin::Category). Scaffolded views
also wouldn’t work as it seems that generator doesn’t take into
account the fact that you are using a
namespace.