Archive

Archive for the ‘rails’ Category

Rails Functional Testing of mime types other than HTML (XML, JSON etc)

December 16th, 2008 Dante Regis 2 comments

I noticed something interesting these days. Even though most Rails websites boasts the benefits of Test Driven Development, most examples they give us don’t test the whole app functionality. 

How come? Well, REST was the word of the day, it is now deep inside our brains, right? But have you ever seen an example test of the XML output of Rails? Surely not. Every guide, tutorial and example shows only how to test for the default output, which is HTML. 

So what? That ought to be easy, right? Just add :format after the get, post, put or delete function and you will be fine, you must think. Well, I have been working on a project and had to test if the Atom Feed would work ok. I must say it was one of the most frustrating tests I have ever written. 

Turns out that, even though you can do something like this:


formatted_posts_url(:format => :atom)

you CAN’T do that on testes:

get :index, :format => :atom

 

You know why? Because, even though you CAN use symbols (:atom) on your code, you CAN’T use on tests FOR THE FORMAT. Let’s rephrase it: You can use symbols on tests when specifying the action, the parameters and everything else BUT NOT THE FORMAT of the request. So the code above will give you a mysterious HTTP 406 Error Code.

So, to prevent you from loosing a full day where you felt productive, like I did, the following code will work:

get :index, :format => "atom"

Best!

Categories: programming, rails Tags:

Factory Girl and has_many / has_many :through associations

November 19th, 2008 Dante Regis 7 comments

If you want to create has_many and has_many through associations in factory_girl as I once did, you will need to put it inside square brackets:

Factory.define :user do |user|
  user.name "My Name"
  user.groups {|groups| [groups.association(:group)]}
end

This will ensure that user.groups is not overriden by a single Group object, but by an array with a Group inside.

Categories: programming, rails Tags:

autotest keeps running continuosly even without changing any files?

December 21st, 2007 Dante Regis No comments

So, your autotest also keeps running over and over when your Rails code fails? Well, I had this issue here and found that the problem was that, when the tests were run, Ferret updated the index. So, autotest believed that files where changed when they weren’t. Googling a bit I found this code that you should put on your ~/.autotest :

Autotest.add_hook :run do |at|
  at.exceptions = /^(?:\.\/)?(?:db|index|doc|log|public|script|tmp|filestore|vendor\/rails)|\.svn|(?:.*_flymake\.rb$)/
end

If it does not work, try running autotest with “-v” option, to see which files have changed on autotest concern, and include the relevant directory on the list above.

Hope this helps you! :)

Categories: rails, ruby Tags:

Formatting dates nicely in Rails

August 16th, 2007 Dante Regis No comments

I always thought those “today”, “yesterday”, “4 days ago” date references on some websites way cool. On a small system I’m working on I did some code to do something alike, though in portuguese. But if you want it in english, there’s a small plugin called relative_time_helpers that you can install by typing:

 script/plugin install http://ar-code.svn.engineyard.com/plugins/relative_time_helpers/

Then, you can do things like

<%= relative_time(Time.now) %>
# today

<%= relative_time(1.day.ago) %><
# yesterday

<%= relative_time(1.day.from_now) %>
# tomorrow

<%= relative_time_span([Time.now, 5.days.from_now]) %># May 17th - 22nd

This tip was taken from Active Reload Blog!

Categories: programming, rails, ruby Tags:

ruby on rails reference cheat sheet

March 7th, 2007 Dante Regis No comments

This website has an excelent Ruby on Rails resource for those in need (like most of us). It features lot’s of command line options, code snippets, and stuff like that. Very handfull! This is the link to the blog entry, and this one points to the text itself. Take a look!

Categories: programming, rails, ruby Tags:

ruby on rails ide comparison

March 7th, 2007 Dante Regis No comments

Sébastien from The Nameless One, did a very impressive job comparing three of the main Ruby on Rails IDE avaiable. His extensive post includes a feature chart with each IDE’s most important features side-by-side, using colors to show how good or bad implemented that feature is on a particular IDE. It’s more than worth looking, and the link is here.

BTW, I’ll stay with Netbeans Development Edition. Even being unstable (I believe it’s not even Alpha yet) it’s quite usable, and I’m doing most of my work on it. Just missing a few test features. But that should come on the next milestones!

Thanks Sébastien, for the great job!

Categories: netbeans, programming, rails, ruby Tags:

how to install ruby on rails and lighttpd on ubuntu

March 6th, 2007 Dante Regis 2 comments

Hey folks. It’s interesting how difficult it was for me to get Rails up and running on Lighttpd. I’ve tried lot’s and lot’s of tutorials, but none of them seemed fit to me. I came up with this solution, wich is actually a merge of lot’s tips. Feel free to contribute, and I hope that this helps you out.

1) First, include UNIVERSE on your sources.list. It is located on /etc/apt/sources.list. If you are unsure, take a look at this web-site. It may help you out. Remember to sudo, or be root to do everything Read more…

Categories: howto, rails, ruby, ubuntu Tags:

netbeans and UTF8 encoding

February 26th, 2007 Dante Regis 11 comments

[digg=http://www.digg.com/programming/Netbeans_and_UTF8_encoding]
When I started using Netbeans as my Ruby on Rails IDE, I noticed that, at least on my build, it did not use UTF 8 on files by default. If you speak english that usually is not a problem, but for me, who write software in portuguese, it is very important.
But, Mr. Google gave me a tip, and I’ll place it here so you don’t have to bother him again with this same question.
Theres a file, inside the /etc folder of netbeans directory, called netbeans.conf. I don’t know if it stays elsewhere in Linux, I’ll only know later today, but anyway, you should place this option inside the quotes of netbeans_default_options:
-J-Dfile.encoding=UTF-8
That’s it. Worked perfectly for me.
Best

Categories: netbeans, programming, rails, ruby Tags:

netbeans and ruby on rails

February 26th, 2007 Dante Regis 3 comments

Tor Norbye is doing a great job integrating a Ruby module on the development version of Netbeans. Since I’m still on the look for my definitive Ruby IDE, I decided to take a look at it, and all I can say is that I liked it very, very much. Never had any experience with Netbeans before (I don’t speak Java), but it’s a great software. The Ruby integration is, as far as I can tell, quite good, only a few bugs showed up on 3 days of work.

You can download it from the Netbeans CVS, the standard module. This post from Tor shows how to get and compile it. It took me long 36 hours to checkout the whole source. Don’t know if it was my Internet connection (yes, Telemar, it’s your fault, not mine, DSL provider from hell), or a busy server, but it was long, and I had to restart the process lot’s of times (around 7 times. But you don’t loose your job, it continues from where it stopped).

Give it a try! And, Tor, thank you very much for the great job!

Best!

Categories: programming, rails Tags:

ruby on rails and javascript modal dialogs

February 24th, 2007 Dante Regis No comments

Javascript offers you a way to build modal dialogs on web pages. You must have seen it on one of these Web 2.0 sites: you click somewhere, a small box appears from nowhere and prevents you from clicking anywhere else, but on itself. It’s cool!

Rails is full of those one-liners to help our life. Mix them together and you have a one-liner for these boxes. It’s a plugin, and it’s called Redbox. Take a look at it!

Categories: rails Tags: