URL/Site structure


#1

Greetings,

I’m fairly noob with rails and making my first DB driven site with it.
I’m using the scaffold generator to develop the admin side of my site
(admin_controller)

I have three sections that I want to have the administrator edit, and I
want to call these from the admin controller that has a layout with
navigation to these three sections.

When using the scaffold generator, it obviously generates the news_items
controller, and puts in the associated methods and makes the views.

What I suppose I am wanting is to have all this functionality called
from the admin controller all utilizing the admin view

class AdminController < ApplicationController
CRUD functions for news_items
CRUD functions for email
CRUD functions for users
end

so that myapp.com/admin/newsitems or myapp.com/admin/email goes to the
admin for that section instead of having it go to
myapp.com/news_items/list (which doesn’t use the admin layout)

So I hope this makes some sense, but I thought it would be smarter to
call all from the admin controller instead of dividing out each section
and having a controller for each. Is there a smarter way to put this
all in my admin controller or should I just control everything with
layouts and routes?


#2

Actually, I am pretty sure I want all this in my admin controller, as
that is where my user authentication is that would block someone from
using the other controller functions.


#3

On Jan 7, 2006, at 6:11 PM, Jason P. wrote:

admin for that section instead of having it go to
myapp.com/news_items/list (which doesn’t use the admin layout)

You can do it all in your admin controller if you want. Just add methods
like:

class AdminController < ApplicationController
def create_news_item
# create your news item
end

def delete_news_item
# delete your news item
end

def create_user
#create a user
end

…etc, etc.

end

Then you can access them all from myapp.com/admin/create_news_item, etc.
But if you’re going to have a full suite of CRUD actions for each thing,
it may be more maintainable to have separate controllers. If you want
them to be grouped together, you can make an admin module. Create a
directory app/controllers/admin/ and put all your admin-related
controllers
there. Then your controller classes would look more like:

class Admin::NewsItemController < ApplicationController
# ^^^^^^ Note the module prefix there

layout ‘admin’ # use app/views/layouts/admin.rhtml for our layout

def create
#create a news item
end

def delete
# delete a news item
end

and so on…

end

-dudley


#4

In your news_items_controller.rb did you define index and list?

ie.
def index
blah blah
end

Jason P. wrote:

Ok I did this, and go to URL

http://localhost:3000/admin/news_items
or
http://localhost:3000/admin/news_items/list

and get the same error for both

‘Unknown action
No action responded to news_items’

is it something wrong with my structure yet?

I have my folders like this:

[-] (‘Admin’ folder)
news_items_controller.rb
admin_controller.rb (up one level next to ‘Admin’ folder)

This is in my news_items.controller:

class Admin::NewsItemsController < ApplicationController
(scaffold code)

and then up one level in my admin_controller.rb I have the user
authentication needed to get into the admin functions.


#5

Okay, I got that figured out: It was because I had an admin controller
and an admin folder (module)

I moved admin_controller into the admin folder and renamed it to
base_controller. But now how do you call an index function on a module?

Jordan I. wrote:

In your news_items_controller.rb did you define index and list?

ie.
def index
blah blah
end


#6

Ok I did this, and go to URL

http://localhost:3000/admin/news_items
or
http://localhost:3000/admin/news_items/list

and get the same error for both

‘Unknown action
No action responded to news_items’

is it something wrong with my structure yet?

I have my folders like this:

[-] (‘Admin’ folder)
news_items_controller.rb
admin_controller.rb (up one level next to ‘Admin’ folder)

This is in my news_items.controller:

class Admin::NewsItemsController < ApplicationController
(scaffold code)

and then up one level in my admin_controller.rb I have the user
authentication needed to get into the admin functions.


#7

On Jan 7, 2006, at 7:50 PM, Jason P. wrote:

‘Unknown action
No action responded to news_items’

is it something wrong with my structure yet?

If you have an admin_controller.rb in the controllers dir, rails will
look for your actions as methods in that controller, instead of in the
admin module, I think. Try removing that controller and moving the
authentication stuff somewhere else. You might take a look at Typo*,
it uses a similar structure for its admin stuff.

-dudley


#8

Thanks for your help so far everyone.

One thing I am having trouble grokking in Rails yet is Routing. I
looked at the Typo example that you posted Dudley, and mine is set up
similar with the ‘admin’ folder and ‘news_items’ controller inside.

in the news_items_controller.rb I have before_filer :login_required
which then routes the url to admin/login which returns:

Recognition failed for “/admin/login”

up one level from the admin folder I have accounts_controller.rb that
has the login functions from the login generator (setup like Typo -
http://typo.leetsoft.com/trac/)

And I look at the Typo example and there are no special configs in
routes.rb that look like it maps that in any way.

So basically the url: site.com/admin(folder)/news_items
has: before filter :login_required
which re-routes to: site.com/admin/login and throws the error above.

In Typo the accounts_controller which does this is up one level, but in
base_controller there is the same before_filter but I can’t find
anything fancy that routes it back to the accounts_controller.

Thanks in advance, I feel once I can get my head around the routing of
rails things are going to come together fast here.

Dudley F. wrote:

On Jan 7, 2006, at 7:50 PM, Jason P. wrote:

‘Unknown action
No action responded to news_items’

is it something wrong with my structure yet?

If you have an admin_controller.rb in the controllers dir, rails will
look for your actions as methods in that controller, instead of in the
admin module, I think. Try removing that controller and moving the
authentication stuff somewhere else. You might take a look at Typo*,
it uses a similar structure for its admin stuff.

-dudley


#9

On Jan 8, 2006, at 3:57 PM, Jason P. wrote:

In Typo the accounts_controller which does this is up one level,
but in
base_controller there is the same before_filter but I can’t find
anything fancy that routes it back to the accounts_controller.

in Typo, this method:

def access_denied
redirect_to :controller=>"/accounts", :action =>“login”
end

from lib/login_system.rb does the routing back to the
accounts_controller.
So you could add a redirect_to wherever your system determines that a
user
isn’t logged in.

-dudley


#10

On Sunday 08 January 2006 04:57 pm, Jason P. wrote:

In Typo the accounts_controller which does this is up one level, but in
base_controller there is the same before_filter but I can’t find
anything fancy that routes it back to the accounts_controller.

Thanks in advance, I feel once I can get my head around the routing of
rails things are going to come together fast here.

Hi Jason,

I can’t “tell” you about routing, but I can tell you a really nice and
simple
way to understand in a few minutes: make a tiny app and then experiment
with
config/routes.rb. Here’s what I’d do starting in a directory where you
have
read/write/execute:

rails rtest
cd rtest
ruby script/generate controller Routetest index page2
ruby script/server

Once you’ve done that, use a browser to pull in the following URL’s:

Now modify config/routes.rb. Try changing

map.connect ‘:controller/:action/:id’
to
map.connect ‘gratuitous-directory/:controller/:action/:id’

Notice that you now access the same pages as
http://localhost:3000/routetest/page2, etc.

Now try commenting that line in routes.rb and uncommenting this one:

map.connect ‘’, :controller => “welcome”

Notice that now you can pull up the main page like this:

http://localhost:3000/routetest

but you cannot pull up page2.

Then try this:

map.connect ‘:action’

and notice you can pull up both pages from http://localhost:3000.

I think the trick is to find a simple app that works all the time, and
then
experiment with routes.rb.

SteveT

Steve L.
Author: