To migrate your database in production environment simply run the following command.
rake db:migrate RAILS_ENV=production
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!
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
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
This helper method will return the information about any object in YAML format, which is very human readable. To use this command, just type:
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.
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.
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! :)
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.
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?
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:
Recent comments
3 days 6 hours ago
3 weeks 1 day ago
3 weeks 5 days ago
4 weeks 4 days ago
5 weeks 3 days ago
7 weeks 2 hours ago
8 weeks 5 days ago
14 weeks 22 hours ago
18 weeks 3 days ago
25 weeks 5 days ago