Five Rails Tips

6 05 2008

I’m a big fan of Ryan Bates’ Railscasts. He is holding a contest and I thought I would participate and share 5 Rails tips with the community.

Without further ado, here are my five tips.

Tip 1: Rake task to remove tildes (~) added by text editors

My favorite text editors (emacs or gedit) create files that end with a tilde character (~) as backup copies when I modify a file. Thus, after working on my rails project for a while, I have a bunch of files ending with ~ that are hanging around. At times, it can be annoying to have my directories filled with these files so I use a rake tasks to get rid of them all.

Put the following in a file called ’tilde.rake’ and save this file in your rails project under the lib/tasks/ directory.


desc 'Deletes all files that end with tilde (~)'
task 'tilde' do
files = []
Dir.glob('**/*~').each do |file|
File.delete(file)
files << file
end
puts "Deleted the following files: #{files.join(', ')}"
end

To run the task, simply run the following rake tasks from your shell:

rake tilde

Update: See Dominic’s comment. Rake already includes a task to clean-up files. Just need to include ‘rake/clean’. Thanks Dominic.

Tip 2: Small Erb Tip

Did you ever need to display <% or %> in an erb template but didn’t know how to do it? I’ve been there!

It’s quite easy actually, all you have to do is use <%% and %%> instead and the erb templating engine will replace them with the appropriate <% and %>.

Tip 3: Detect Cross-Script Scripting (XSS) Attacks ‘Holes’

If you do not know about XSS attacks, panic and go read this chapter of the Rails manual to learn about them.

Rails provide the h() helper method for HTML meta-character conversion in views. It’s important to use it whenever you are inserting dynamic data in your erb templates.

Sometimes, I want to double check that I’ve been using the h() helper methods everywhere so I want a list of everywhere I’ve used <%= in my views. I use the following command to scan through all the <%= where I’m missing the helper method:

grep -R '<%=' app/views/* | grep -v '<%=h' | grep -v '.svn' | more

Rather than typing that command everytime, I simply created an alias for it by saving the following in my .bash_aliases file:

alias xss_erb_scan="grep -R '<%=' app/views/* | grep -v '<%=h' | grep -v '.svn' | more"

Tip 4: gEdit on Rails

My laptop runs Ubuntu so I use gedit for doing Rails development. gedit is highly configurable and you can turn it into a bad-ass Rails-IDE with a few tweaks.

First follow the steps on this tutorial. This will make your gedit environment pretty much ready for rails development.

Then, you can add the ability for gedit to execute Ruby code that you have in your currently open document by just using a keyboard shortcut. To do this, first install the ‘External Tools’ plugin. Go to ‘Edit’, then ‘Preferences’, and select the ‘Plugins’ tab. Find and select the ‘External Tool’ plugin and click on ‘Configure this plugin’. The following window should popup.

Click on the ‘New’ button and create an ‘Execute Ruby’ tool by filling in the information as shown below:

That’s it. You can close the window and test by writing some ruby code in a new document and pressing F5. The code will execute and the output will be shown at the bottom of the editor.

Tip 5: Explore Other Ruby Web Frameworks

My last tip is to explore other ruby web frameworks. There are a lot of other web frameworks written in Ruby and you can learn a lot by just exploring them. Even if you do not plan to use them for a production site, try using them for a small project, look inside their codebase and try to understand the code. Since Rails is a more mature and more complex framework, it can be scary to look inside the Rails code but the same can’t be said for some of the newer, lighter frameworks out there. Furthermore, exploring these other frameworks can give you new ideas when working on Rails projects. So just like the pragmatic programmers advocate learning one new language a year, I recommend learning a few web frameworks in that language during that year to expand your knowledge of the language and avoid tunnel vision. So go start exploring Merb, Ramaze, Camping, Sinatra, etc… Check here for a full list of Ruby web frameworks.

I hope you will find some of these tips useful. I look forward to read the tips submitted by the other railscast readers.





Install Java On Ubuntu

21 09 2007

It is so easy to install Java on Ubuntu now. On Ubuntu 7.04 (Feisty Fawn), follow these steps:

1) Make sure you have the right repositories on your sources.list file.

Edit your sources.list file the following way:

sudo gedit /etc/apt/sources.list

or if you are not using gnome (e.g. you are logged in through ssh):

sudo nano /etc/apt/sources.list

If the following lines are not already there, add them:

deb http://us.archive.ubuntu.com/ubuntu feisty main restricted
deb http://us.archive.ubuntu.com/ubuntu feisty universe multiverse

Save the file (in nano use Control-O to save and then Control-X to exit).
Now you need to tell the package manager to update it’s database:

sudo apt-get update

2) Install Java using apt-get

I currently have both Java 5 and Java 6 installed on my machine and it’s very easy to switch between the two. Java 6 has some nice goodies such as JSR-223 and better jconsole that I like to use.

For Java 5:

sudo apt-get install sun-java5-jre sun-java5-jdk sun-java5-plugin

For Java 6:

sudo apt-get install sun-java6-jre sun-java6-jdk sun-java6-plugin

There are other packages that you might be interested in:

  • sun-java6-demo
  • sun-java6-doc
  • sun-java6-source

3) Verify installation

Verify that you have the correct version of java running:

java -version

For example, on my machine (yes, I call it tiwouj):

Ubuntu Java Version

To see a list of the java versions that you have installed, you can use the following command (it’s a letter L at the end of the command):

update-java-alternatives -l

Here is my list:

Ubuntu Java List

This should show you a list of all the java alternatives you have installed. java-gcj is the free version of Java from the GNU project and comes installed by default with Ubuntu so you should see it. To switch to a different version, use the following command:

sudo update-java-alternatives -s <version-desired>

e.g:

sudo update-java-alternatives -s java-1.5.0-sun

Make sure it worked:

java -version

That’s it! How easy was that? You have to love apt-get.