1.8.x Ruby on Rails RESTful nested admin page and form_for problems

Posted by Loomer on Stack Overflow See other posts from Stack Overflow or by Loomer
Published on 2010-04-22T14:55:18Z Indexed on 2010/04/22 15:03 UTC
Read the original article Hit count: 200

Filed under:

So I am creating a website that I want to have an admin directory in rails 1.8.x and I'm struggling a bit to get the form_for to link to the right controller. I am trying to be RESTful. What I basically want is an admin page that has a summary of actions which can then administer sub models such as:

/admin (a summary of events)
/admin/sales (edit sales on the site)
/admin/sales/0 (the normal RESTful stuff)

I can't use namespaces since they were introduced in Rails 2.0 (production site that I don't want to mess with updating rails and all that).

Anyway, what I have in the routes.rb is:

map.resource :admin do |admin|
  admin.resources :sales
end

I am using the map.resource as a singleton as recommended by another site.

The problem comes in when I try to use the form_for to link to the subresource RESTfully. If i do :

form_for(:sales, @sale)

it never links to the right controller no matter what I try. I have also tried:

form_for [@admin, @sale] do |f|

and that doe not work either (I am guessing since admin is a singleton which does not have a model, it's just a placeholder for the admin controller). Am I supposed to add a prefix or something? Or something into the sales controller to specify that it is a subcontroller to admin? Is there an easier way to do this without manually creating a bunch of routes? Thanks for any help.

© Stack Overflow or respective owner

Related posts about ruby-on-rails