Nested Controllers and Broken link_to

So my first little rails app is taking it’s baby steps in the wild today, and some users should hit the site over the weekend… Exciting stuff.

I’ve written an admin area for the site, and with only a tentative grasp on the whys and wherefores, I’ve copied something I saw in the mephisto source code and used ‘nested controllers’ to keep the admin code tucked away from the rest:

$script/generate controller admin/users
$script/generate controller admin/stuff

For want of a better way, I put a base class for the admin controllers into the application.rb file (can’t remember where I picked this tip up) and put a one-line before_filter on that class to check whether the user has the appropriate rights. Sweet.

The little gotcha with this was that a lot of my existing link_to and redirect_to calls didn’t work from within the admin area, because I guess the :controller value in the options is relative. Adding a forward-slash to the controller name fixes it up:

<%= link_to 'home', :controller => '/home' %>

Looks like not the only one to have hit this little bump.

I think I need a new rails book: I’m starting to leave my trusty pragprog book behind. Any recommendations out there people?

Published by Matt

I write software, and love learning how to do it even better.

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *