Master ruby with us

Archive for January, 2009

‘cycle’ css-related feature of Rails

Posted by firstruby on January 14, 2009

If you want to display the list ( of users ) with different css-class for alternate record, there is no need to check the odd-even records,

For this, Rails have awesome feature and that is – ‘cycle’

We can use this as,

say we have 2 css classes-blank and white. If want to apply ‘black’ and ‘white’ class for alternate user record, then we have to write the code as,

<% @users.each do |user| %>
<li class="<%= cycle('black', 'white') -%>">
<%= %></li>
<% end %>

Cool feature 🙂

Posted in Useful Links | Leave a Comment »

Creating Sub Controllers in RubyOnRails

Posted by firstruby on January 14, 2009

Previously I was adding most of the code in one controller…say if there is admin module, and there are different sections in it, then it’s better to add the code in separate controllers – sub-controllers.

Here is the way to create sub-controllers,

ruby script/generate controller admin/users

It will create users_controller.rb in /controllers/admin folder (sub-folder for controller)

Your user controller will look like,

class Admin::UsersController < ApplicationController

And to inherit the properties of the parent controller (AdminController), you can change parent controller as AdminController instead ApplicationController

class Admin::UsersController < AdminController

Hope this helps.

Posted in Useful Links | Leave a Comment »