Move to namespace

35 views
Skip to first unread message

Commander Johnson

unread,
Sep 20, 2008, 8:02:09 AM9/20/08
to rubyonra...@googlegroups.com
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

Erol Fornoles

unread,
Sep 20, 2008, 8:23:54 AM9/20/08
to Ruby on Rails: Talk
On Sep 20, 8:02 pm, "Commander Johnson" <commanderjohn...@gmail.com>
wrote:
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

Erol Fornoles

unread,
Sep 20, 2008, 8:34:27 AM9/20/08
to Ruby on Rails: Talk
I think I got it all wrong. You want a fast way of refactoring from
one namespace to another?

Commander Johnson

unread,
Sep 20, 2008, 8:38:29 AM9/20/08
to rubyonra...@googlegroups.com
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.

Commander Johnson

unread,
Sep 20, 2008, 9:45:59 AM9/20/08
to rubyonra...@googlegroups.com
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?

Commander Johnson

unread,
Sep 20, 2008, 9:57:08 AM9/20/08
to rubyonra...@googlegroups.com
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

Erol Fornoles

unread,
Sep 20, 2008, 11:13:12 AM9/20/08
to Ruby on Rails: Talk
On Sep 20, 9:57 pm, "Commander Johnson" <commanderjohn...@gmail.com>
wrote:
> 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

Commander Johnson

unread,
Sep 20, 2008, 5:59:45 PM9/20/08
to rubyonra...@googlegroups.com
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?

Erol Fornoles

unread,
Sep 20, 2008, 7:23:58 PM9/20/08
to Ruby on Rails: Talk
On Sep 21, 5:59 am, "Commander Johnson" <commanderjohn...@gmail.com>
wrote:
> 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.

Commander Johnson

unread,
Sep 20, 2008, 8:18:44 PM9/20/08
to rubyonra...@googlegroups.com
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>

Erol Fornoles

unread,
Sep 20, 2008, 8:40:43 PM9/20/08
to Ruby on Rails: Talk
On Sep 21, 8:18 am, "Commander Johnson" <commanderjohn...@gmail.com>
wrote:
> 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...
>
> 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
Reply all
Reply to author
Forward
0 new messages