Five Rails Tips
6 05 2008I’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.
Tags : ruby rails railscasts
Categories : Ruby, rails


