Quick Tip: How to fix ‘rake/rdoctask’ is deprecated. use rdoc/task instead

If you’ve recently found rails complaining about the following:

rake/rdoctask is deprecated. Use rdoc/task instead (in RDoc 2.4.2+)

then open up your rakefile.rb and change

require 'rake/rdoctask'

to look like this:

require 'rdoc/task'

and remember to add the following to your Gemfile


group :development do
gem 'rdoc'

end

You may need to run the following on the command line to freshen your gems

sudo bundle install

Rails selection lists

Problem: I’ve got the need to have a list of options presented in a drop down list but I really don’t mind them being hard coded somewhere (if I did I’d stuff them in a database table).

Fix: Easy I can add an array of arrays to my model file and them pull them through to the view. In each sub-array the first element in the name displayed, the second is that submitted with the request.

Example: In the model for my bookings I add booking_status

class Booking < ActiveRecord::Base

  BOOKING_STATUS = [
    ['booked', 'booked'],
    ['request', 'request'],
    ['pending', 'pending'],
    ['approved', 'approved'],
    ['not-approved', 'not_approved'],
    ['other', 'other'] ]

end

In the view I can then include a selection tag which uses each of these items

<%= f.select :status, Booking::BOOKING_STATUS %>

Rails rapid prototyping – partials

Rails script generators enable true prototyping from the command line. The simplicity of being able to generate a complete working application within a few commands is wonderful. However I often find that I’m spending time to ensure some very simple basics are better than the out of the box offering.

Yes I know I prototyping, yes I know I’m going to through it away at the end of the day but still there is no need for not making effective use of partials in forms.

Partials within forms makes sure functionality is rapidly updated in both the new and edit actions.

So typically I like to convert:

<h1>Editing booking</h1>

<% form_for(@booking) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :event_id %><br />
    <%= f.text_field :event_id %>
  </p>
  <p>
    <%= f.label :user_id %><br />
    <%= f.text_field :user_id %>
  </p>
  <p>
    <%= f.label :status %><br />
    <%= f.text_field :status %>
  </p>
  <p>
    <%= f.submit 'Update' %>
  </p>
<% end %>

to something which makes much more sense like:

<h1>Editing booking</h1>

<% form_for(@booking) do |f| %>
  <%= render :partial => 'form', :locals => { :f => f } %>
  <p>
    <%= f.submit 'Update' %>
  </p>
<% end %>

I’d encourage you to do the same and see how it saves you one more piece of hassle. And of course if your using text mate remember ctrl+shift+H will create the partial for you.

Rails localisation bug

I’ve just gone through the challenge of working out what was wrong with the following piece of code:

<% form_tag :controller => '/' do %>
				<%= t(:'txt.language') %> <%= select_tag("locale", options_for_select(LOCALES_AVAILABLE, I18n.locale), { :index => nil, :o nchange => 'this.form.submit()'}) %>
			<% end %>

Our client reported that on a signup page where they choose an alternative language it got redirected and they could never reach the page in their native language.

How can this happen you might ask?

Well the request includes some values within the url which are used as a key. Only if the key is valid is the controller allowed to complete the request. The issue is the combining of maintaining the key and switching the language.

As it happens its a very simple fix and was pointed out to me by Juliana.

<% form_tag :controller => request.request_uri do %>
				<%= t(:'txt.language') %> <%= select_tag("locale", options_for_select(LOCALES_AVAILABLE, I18n.locale), { :index => nil, :o nchange => 'this.form.submit()'}) %>
			<% end %>

In this fixed second example the alteration for the controller has been updated to direct the language selector to the controller of the current page. The documentation for this suggests that its broken on IIS (really should you be even trying to use this?) in fact I found that the source has had a significant amount of work go into it to work around the issue and even make it work on that webserver. Its a great little solution to ensure that the reloaded page is returned to the user in the state it was with the addition of the correct language.

Rails – if I new when I started what I know now?

How would you answer this question?

I’ve been using rails for development of applications for what feels like a long time. I wasn’t in the pre 1.0 crowd but I did spend many days hacking around in cgi scripts to make the website run in apache so I do feel like I’ve been here a while.

As brackground I came up through C++ operating systems development (loved it), delphi (blah blah), then on to Java (which I never liked) and into web technologies and scripted languages.

My day job doesn’t allow more than 10-20% of development anymore (by choice). So although I’m still keen the time is not usually available. Interesting this has quite an impact on way you program.

For one thing change is an issue, changing technology requires great investment just to achieve the simplest of tasks. Something I can not afford.

Rails has always been for me about using a good language to enjoy my craft. For a variety of reasons enjoyment does enable better results. Further the rails stack goes all the way through the systems I need to utilise – database, ORM, MVC, and client side (javascript + tempaltes). By understanding one language and leveraging the well written information on Rails I avoid needing to care about most other areas.

However the trade off is that sometimes it just can’t do what you need so some custom javascript or SQL query is required. I’ve never been particularly strong at either. So that usually gets less of my attention. I break the rules at this point and do what needs to be done to get the job done. Many in the community would disagree with this approach. And I applaude them, they are correct but realities/needs/timescales differ. As yet I’ve never had a hard time sleeping at night!

In many cases the Agile method of working is excellent. I was slow to value the testing framework built into the system. For a while I played with Selenium (amazing solution) but I have a regularly changing interface and could never get beyond the issue that tests broke because they were out of date (not the application was broken). Shoulda on the other hand has been very helpful as has rcov.

Of course the testing approach when time constrained is brief in some aspects. And not by design. Its just their is no documentation on how to achieve certain things – e.g. validate a upload dialog can import, parse and process an zip file + manifest. Of course this is domain specific but validation of file uploads is not, and as yet I’ve not found anything on this.

Google is a strong friend when there is trouble. I’ve found many articles and blogs to guide me. Although sometimes in the wrong direction – engines. There coming back and I’m scared I found them confused and difficult the first time around so I wonder (without a name change) who will be listening?

Would I do it all again: yes. In fact I’d do more. Its a fantastic stable platform which has I believe shaken up the entire ‘intelligent’ web development community along with its cousins DJango I’m looking at you. I’m sure that even in these difficult times we’ll continue to see this is a fertile ground with many innovations and improvements yet to come.

Looking to the future I’m very aware of the tiny amount of knowledge I have. I’ve spoken to many in the community who are streets ahead. I wonder if I’ll ever get to where they are? Do I need to perhaps not, if anything I can say that even with a small amount of knowledge and clear understanding of the principles (which you can pick up very quickly) creating the next application is available to almost any developer.

If only rails could create cross platform desktop applications?


Localising a rails app – in 20 mins

I had an existing rails app which has nearly completed and during its development its been upgraded from 1.2 all the way up to 2.2.2. Anyway I sat down with the latest updates and figured that acts_as_paranoid and will_paginate needed to be altered but beyond that it seems it just worked.
Suffice to say I’d managed to configure the locales and make heavy use of google translate to begin adding an alternative language.
It is impressive how easily this has come together.