Lightbox in rails

Using a lightbox in rails is just a few steps.

Download lightbox.js and lightbox.css (or) control.modal.js and control_modal.css, which will be available if you search in any search engine. Copy the js file to your javascripts folder in the app and copy the css file to your stylesheets folder.

Then do the following code in your view file

<%= javascript_include_tag ‘prototype.js’, ‘lightbox.js’%>
<%= stylesheet_link_tag ‘lightbox’%>

<span id=”lightbox”><a id=”call” href=”<%= new_personal_path %>”>Click Here to Add a New Employee’s Personal Detail</a></span>
<script>new lightbox($(‘call’), {className:’lightbox_container’});</script>

if you use control.modal.js instead of lightbox.js then do this

<%= javascript_include_tag ‘prototype.js’, ‘control.modal.js’%>
<%= stylesheet_link_tag ‘control_modal’%>

<span id=”Control.Modal”><a id=”call” href=”<%= new_personal_path %>”>Click Here to Add a New Employee’s Personal Detail</a></span>
<script>new Control.Modal($(‘call’), {className:’lightbox_container’});</script>


Making a ajax request from rails

Making a AJAX request in rails is pretty simple. Just follow the below example. It will take you to the particular  method (ex.., todo_fix method in the rooms controller) as used in my example. Then in controller you can perform your operation and do the render part.

In View File

Sample Rails code

<% todo_item=TodoItem.find(:first) %>

<a href=”javascript:call_this(<%= %>);”>Add a New To-Do Item</a>

JavaScript function in the same file

function call_this(a)
var url = “/rooms/todo_fix”;
var pars = “new_todo=” + a;
var target = ‘idresult’;
var myAjax = new Ajax.Updater(target,url, {method: ‘post’,parameters: pars});

Conditions on Rails 3 is very simple

Post.where(:title => “test”)
Post.where(:title => “test”).order(“created_at DESC”)
Post.where(“created_at <= ?”,
Post.where(:author => “Joe”).include(:comments).order(:title).limit(10)

Validation in Rails 3

  • :presence => true
  • :uniqueness => true
  • :numericality => true
  • :length => { :minimum => 0, maximum => 2000 }
  • :format => { :with => /.*/ }
  • :inclusion => { :in => [1,2,3] }
  • :exclusion => { :in => [1,2,3] }
  • :acceptance => true
  • :confirmation => true


validates :login, :presence => true, :length => {:minimum => 4},
          :uniqueness => true, :format => { :with => /[A-Za-z0-9]+/ }

Ajax Request in Rails 2 & Rails 3

AJAX request from anchor tag

Rails 2
<a href="#" onclick="new Ajax.Request('/comments/1', {asynchronous:true, evalScripts:ture, method:'delete'}); return false;">Destroy</a>

Rails 3
(unobstrusive JavaScript in Rails 3) 
<a href="/comments/1" data-remote="true" data-method="delete">Destroy</a>

Routing in Rails 3

Root Route

root :to => 'welcome#show'.

Named Route

match ‘logout’, :to => ‘sessions#destroy’

RESTful Routes

resources :products

Nested Resources

resources :projects do

resources :tasks, :people


Namespaced Resources

namespace :admin do

resources :projects


Some example :

  match "/about(.:format)" => "info#about", :as => :about

  match "/:year(/:month(/:day))" => "info#about", :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }