rails

Migrate Rails Database

To migrate your database in production environment simply run the following command.

rake db:migrate RAILS_ENV=production

Tip: Rails Debug Helper in a Popup

I found this nice article which proposes a ready-to-use debug popup window. All you have to do is add a simple command at the end of your application layout and a popup will show all the information regarding the request, session, response, flash variables and more.

Quick way to debug your web application.

Cheers!

Rails Flash message on the current request only

Sometimes all you want is to display a flash message for the current request template, i.e. it should not propagate to the next action. For example, I had an action where I wanted to warn the user that he had already processed a file:

def parse
    @upload = Upload.find(params[:id])
    if (@upload.processed_at)
      flash[:warning] = "This file has already been processed at " + @upload.processed_at.to_s(:long)
    end
end

Debug Rails View Helpers

Some common tasks in the development business are done no matter the programming language you use. Debugging is one of those tasks. While developing web applications I often need to debug the View layer and it is kind of a tedious job to write while loops to inspect variables values, for example.

Rails provides some interesting helper methods for your views:

  • debug
  • to_yaml

debug

This helper method will return the information about any object in YAML format, which is very human readable. To use this command, just type:

Choosing Layouts at Runtime in Ruby on Rails - iPhone Safari x Firefox or IE on a PC

I wanted to set a different layout in my application depending on a decision of a special situation. For example, if the client system is a browser on an iPhone I'd like to show a different layout than a Firefox running in a PC.

I found inspiration on the official RoR documentation, specifically on the page about the rendering mechanism. Here is the code I wrote to display a specific layout if the client browser is a Safari running in a iPhone or iPod touch.

Rails find :conditions

The Class ActiveRecord::Base provides an interesting method to execute searches on the database without the need to write SQL queries. It is the find method.

The find method has some retrieval options (:all, :first, :last, :id) and many parameters which are detailed in the links above. In this article we'd like to focus on the :conditions parameter.

Lower Case, Upper Case and Capitalize in Ruby

Ruby has some very straightforward methods to change the case of your strings.

Here are some of them:

"HoMer".downcase    # returns homer
"lisa".upcase       # returns LISA
"margie".capitalize # returns Margie
"BaRt".swapcase     # returns bArT

Just a quick one! :)

 

 

 

Ruby on Rails Model Auto Completer

When developing web applications nowadays, caring about user interaction is mandatory. Using Ajax technology to allow your system to show information without reloading the whole page is one of the features every web user is getting used to.

 

One good example on this kind of interaction are those text boxes with auto completion. While the user types some name the system will automatically search for matches to those names in the database, offering sugestions on possible existing values.

 

Running tests in the command line on Ruby on Rails

I normally run the test suite for my Ruby on Rails projects using the Netbeans test interface.
 
Unfortunately this interface is only useful to run the whole suite. It does not offer (as it does for Java) the possibility to run a single test class or a single test from a test class.
 
When I need to this I just go to the command line and type:
 
ruby test/functional/accounts_controller_test.rb
 
But what if I need to run one single test from this test class?

Ruby on Rails Order By Using Associated Model

It is very easy to use an 'order by' clause in a model in Ruby on Rails. Supposing you have a Category model it's as simple as doing this:
 
@categories = Category.find(:all, :order => 'title')
 
But what if I need to order by the title of the subcategories after ordering by the categories title?
 
Using the category_id does not work either, because it almost sure that the categories will not be added in alphabetical order, so the command below does not work for me either:
 

Syndicate content