RESTful admin namespaced controller using scaffolding

Most of my clients prefer to have a separate admin section. In turn, I like to have separate controllers for admin section and front-end in my Rails app. This is not as straightforward as it might seem, especially if you like to use scaffolding for admin controller.

The goal is to get 2 separate RESTful controllers, admin & front-end controller, one model and for admin pages to have scaffolding.

Here is the easiest way I found so far to accomplish this. This example generates categories model and controllers for it.

./script/generate controller admin/categories
./script/generate scaffold category name:string

This will generate an empty controller in admin namespace and a scaffolded resource for front-end controller.

Now we have everything generated we just need to make it work with admin controller and not with front-end.

  • move all templates from app/views/categories to app/views/admin/categories
  • copy all functions from categories_controller.rb to admin/categories_controller.rb
  • add namespace for admin controller in routes.rb:map.namespace :admin do |admin|
    admin.resources :categories
    end
  • in admin/categories_controller.rb replace in 3 places redirect_to calls to work with admin namespace. It will have something like redirect_to(@category), but to work with namespace it needs to have redirect_to([:admin, @category])
  • make similar changes in all templates, i.e. make it work within an admin namespace. You need to make following changes:
    • form_for(@category) => form_for([:admin, @category])
    • <%= link_to ‘Show’, @category %> => <%= link_to ‘Show’, [:admin, @category] %>
    • categories_path => admin_categories_path
    • edit_category_path(@category) => edit_admin_category_path(@category)
    • new_category_path => new_admin_category_path

That’s it. Now you’ll have /admin/categories for all administrative tasks and you have a free controller for front-end actions.

You might wonder why not just generate scaffold for admin/categories… The reason is that you’ll also get a model that is namespaced in admin (i.e. model would be Admin::Category). Scaffolded views also wouldn’t work as it seems that generator doesn’t take into account the fact that you are using a namespace.

Share/Save/Bookmark

Mosso hosting cloud

Mosso’s hosting cloud at $100 / month seems like a good solution to get a scalable server. However one thing bugs me, actually two things…

First one: they offer FTP only access. Meaning you cannot deploy sites directly from code repositories (i.e. git or svn). That sucks.

Second thing that bugs me: for $100 you get quite a lot of computing power which can be used to run multiple sites - but you are only allowed to have one Rails app running. Only one. If you want additional Rails apps (for example to have a test server) you need to pay an extra fee.

I know it’s cloud computing and that you have to be able to run it with any additional configuration (that’s why I think it’s ok that you have to freeze your gems in Rails apps, because you cannot install any gems yourself)… but not being able to checkout my code from repository and having to upload the whole app each time you make changes is really annoying.

In their defense, the support guy said that they are working on it, but he could give me an ETA when they’ll allow something like that.

Share/Save/Bookmark

InnoDB per-table tablespaces - split ibdata1 to smaller chunks

Today I had to import 3GB of InnoDB tables in MySQL. Unfortunately, while importing the server run out of disk space - which caused whole server to grind to a halt. Naively I tried to delete imported data to free up space… I was in for an unpleasant surprise.

By default when MySQL uses InnoDB engine it stores most of the information in single file called ibdata1. One downside is that once ibdata1 file grows it cannot shrink - even if you delete all InnoDB tables. For some reason MySQL is set to use single file instead of per-table tablespaces similar to MyISAM.

Enabling per-table tablespaces is easy just add innodb_file_per_table to my.cnf file. Problem is that all newly created tables, only new tables, will be in separate files. It seems that there’s no easy way to convert old tables and reclaim the space taken by ibdata1.

There are 3 ways and two are basically export-drop-delete-import type of solutions:

  1. Convert all InnoDB tables to MyISAM
  2. Export only InnoDB tables, drop them, delete ibdata1 and import InnoDB tables.
  3. Export all databases, delete ibdata1 and import everything back.

I choose option 2 because I luckily had only 40 InnoDB tables and much more using MyISAM. For details on how to apply each solution and down/up sides of each read MySQL: Reducing ibdata1.

Share/Save/Bookmark

This should be interesting weekly - week 28

[condensed from 'this should be interesting' tumblelog]

Share/Save/Bookmark

Collection partial variable naming - new in edge rails

One thing that always annoyed me when rendering collection partials is that a local var in partial is named same as a partial template name. So, to go around this I always created another local variable in partial and gave it a more meaningful name.

No longer… As the Ryan says from now on in the Edge Rails you can specify the name of the local variable in which each collection element will be exposed within a partial. You will can do this:

render :partial => 'employees', :collection => @workers, :as => :person

It’s a simple thing, but it’s just one of the things that bugged me. I am glad it’s gone now.

Share/Save/Bookmark

This should be interesting weekly - week 27

This is the first weekly condensed edition of my “This should be interesting” tumblelog. Every Sunday I will re-post most interesting posts here on the Icebergist.

Share/Save/Bookmark

Mac OS X problems upgrading from 10.5.3 to 10.5.4

Today I tried to upgrade my Mac from 10.5.3 to 10.5.4 using normal Software Update. That didn’t go as planned as installation hanged at “Configuring Installation” and only thing I could do is shutdown the computer.

After some research and few unsuccessful retries I have finally managed to solve the upgrade problem by downloading standalone 10.5.4 update from Apple download section; booting in safe mode (hold shift key when booting) and then installing the update.

Share/Save/Bookmark

SproutCore - a javascript framework

SproutCore is a javascript framework which tries to enable developers to build web apps that look and act more like a desktop apps.

It steps away from a classic web app model by moving a lot of app into the browser itself, which then interacts with server via AJAX. As it says on the SproutCore site:

After lots of testing, we have found that the most efficient way to server a SproutCore application is as a …. static web page!

This means that a “simple” static HTML page (which is easily served by Apache) makes browser do most of the work (i.e. server doesn’t have to generate the pages) which frees up server to respond only to AJAX initiated requests.

SproutCore is written in Ruby, but once you build the app it will generate a set of HTML, JS and CSS files, so you don’t need to know Ruby in order to use it. As the site says:

The code you write with SproutCore will resemble a desktop app written in Cocoa more than it will a web application written in Rails.

SproutCore Photos Demo ScreenshotAnother great thing about SproutCore is that it can be hooked up with any backend as long as it can communicate with it using HTTP. It can be anything: Rails, PHP, Perl, Java, ASP…

Actions speak louder than words, so take a look at the SproutCore demos which shows you exactly what it’s all about.

In next few days I will try to build a sample application powered by SproutCore and Rails to see how it goes. I will post my impressions here. After all if it’s something Apple used for Mobile.me - well, it can’t be that bad. ;)

Share/Save/Bookmark

So it begins…

Welcome to an icebergist blog. The Icebergist will be a blog about both technical and business stuff I deal with daily. From Ruby on Rails programming to time organizing, everything that’s important in my daily work as a freelance developer.

As you can see I just setup the Icebergist blog, so at the moment it’s content-free. While I write some posts you can checkout my posts from my previous blog AS Workshop.

Also I maintain a tumble log called “this should be interesting” where I post small bits of information which I find useful or interesting.

If that’s not enough for you then check out my Twitter feed where I ramble daily (unless I am to lazy or too busy to do so).

Share/Save/Bookmark

Icebergist blog

Slobodan Kovacevic

Categories

Tags

Blogroll

Subscribe