Hi,
I’ve built the Admin section of my project first, and would like to move it to its own Admin namespace.
I have four resources: Form, Element, Admin and User.
Old URL would be:
localhost:3000/forms
New URL would be:
localhost:3000/admin/forms
How do I migrate from one namespace to another?
After moving I want to create a second FormsController in the default namespace.
I would then script/generate controller and script/generate view and modify as needed.
Looking forward to hear your thoughts about this.
CmdJohnson
Create a folder named "admin" in the following directories:
- app/controllers
- app/helpers
- app/views
Move the controllers, helpers and views which are to be namespaced to
the corresponding admin subdirectory.
You will need to edit the controllers that are namespaced. For
example:
Before: class FormsController < ApplicationController
After: class Admin::FormsController < ApplicationController
And do the same with your namespaced helpers.
Lastly, modify your routes.rb to reflect the namespaced resources:
map.namespace :admin do |admin|
admin.resources :forms
admin.resources :elements
...
end
HTH
I think I got it all wrong. You want a fast way of refactoring from
one namespace to another?
Your first post looks like it’s exactly what I need.
Moving from Default namespace to Admin namespace is what I wanted.
But if there is a faster way, please don’t hesitate to share.
Uh oh.
500 Internal Server Error
Because I already have a Controller named AdminController, I used the namespace Backend.
I organized the files like you said in your first post. Every Controller and every Helper now looks like this:
class Backend::AdminController < ApplicationController
Any ideas?
I experimented with this line in routes.rb:
map.root :controller => :forms
instead of
map.root :controller => “forms”
That caused the 500 error.
Now that last statement is valid, but doesn’t work. How do I make the root path ‘/’ point to Backend::FormsController?
When I log in as admin, I get the error:
undefined method `form_path’ for #ActionView::Base:0x76ec1a
How do I make the *_path method understand what namespace we’re in?
CmdJohnson
You'll need to point to the path of the controller. So for your
Backend::FormsController, the root route should be set as:
map.root :controller => "backend/forms"
Since Backend::FormsController resides on backend/forms.rb
Also, the path and url helpers for your namespaced controllers are now
named backend_{controller}_path. You can confirm this - and check out
all the other routes, as well - by running:
rake routes
In one of my views I used to have the line:
<%= link_to ‘Show’, form %>
Which looked neat. According to ‘rake routes’, I should now be able to use backend_form:
backend_form GET /backend/forms/:id {:controller=>“backend/forms”, :action=>“show”}
But that gives me:
undefined local variable or method `backend_form’ for #ActionView::Base:0x19fae86
So now we have this:
<%= link_to ‘Show’, :controller=>“backend/forms”, :action=>“show” %>
Which works but is less neat.
My app/views directory looks like this:
backend
admin
elements
forms
layouts
users
My controllers are now layout-less. Should the layouts dir be one higher?
You'll need to append _url or _path for the url helpers to work.
Layouts are namespace agnostic. Specifying layout "backend" in your
Backend::ElementsController, for example, would load "app/views/
layouts/backend.erb" as that controller's layout.
Where do I place my layout so I don’t have to specify one explicitly?
I got this line of code:
<% form_for([:backend, @form]) do |f| %>
From here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001740
But still get the error
undefined method `form_path’ for #ActionView::Base:0x1f25d83
If you want all your controllers to use the same layout, you could
just define it at application.rb, like:
class ApplicationController
layout "mylayout"
...
end
Try this form_for construct:
<% form_for @form, :url => {:action => "update"} do |f| %>
Or for new records:
<% form_for @form, :url => {:action => "create"} do |f| %>
HTH