Generating Permalinks

CuMu.Li Comments Off
I’ve just started writing a web app and wanted to have URLs of the form /:year/:month/:day/:permalink. Here’s the routes I’ve ended up using:

  map.resources :rants
  map.rant_permalink "rants/:year/:month/:day/:permalink", 
                    :controller => "rants", 
                    :action => 'by_permalink',
                    :year => /\d{4}/,
                    :month => /\d{1,2}/,
                    :day => /\d{1,2}/,
                    :permalink => /\w(\w|-|_)+\w/

Generating URLs from a model (say for example rant) with a created_at field using the default rant_permalink_url helper would involve doing this:

  rant_permalink_url :year => rant.created_at.year, 
                                :month = rant.created_at.month, 
                                :day = rant.created_at.day, 
                                :permalink => rant.permalink

Very tedious to do that often. So I created a rant_permalink(rant) method (a wrapper for the rant_permalink_url function) in the ApplicationController, however this method isn’t accessible from the views. I put the method in the ApplicationHelper, but then it’s not available from the controller.

And then I scratched my head. I looked through the ApplicationController::Routing code and found that the “#{named_route_name}_url” functions are defined both in ActionController::Base and ActionView::Base. I thought about defining my methods both in the controller and the helper, but decided that wasn’t very DRY. I looked at the Rails doc entries for the helper method (which pulls in other helpers for use in your views) and noticed another method below it, helper_method this is an ActionController class method that aliases methods in a controller to methods of the same name in your helper. HURRAH!

My first plugin

CuMu.Li Comments Off

I’ve been messing about with Rails for about 8 months now. I think it’s a great framework for developing web applications with. However, it does seem that there’s a certain zen aspect to it all. There’s a million and one ways to do things, but often only a few right ways. Anyway, it’s taken me a while to work out for myself what I think are the best ways to do things.

So, cutting the crap—Here’s my first Plugin!

This plugin provides another version of the form_for helper (called ala_form_for) that produces forms in the style of the A List Apart article Prettier Accessible Forms.

The form contains a fieldset, title for fieldset comes from :title attribute in the ala_form_for options hash,

Fields are created by using the normal text_field, password_field etc helpers and when created are preceded by labels which have a default of fieldname.humanize, but can be changed by passing :label => "custom label" to the options hash of the field helper. Fields can also be marked as required by passing :required => true. These label—input pairs are wrapped in <li> elements and these in turn are put into an <ol>.

Errors appear inline with the form element the relate to. I think this makes incorrectly filled in forms more usable. The <li> the form element is wrapped in gains the class fieldWithErrors and an div is added following the input with the class fieldWithErrors containing an unordered list of the errors relating to that field.


<% ala_form_for :tester, @tester, :url => { :action => "new" }, :title => 'Form Title', :html => {:class => 'cmxform'} do |f| %>
    <%= f.text_field(:title, {:required => true}) %>
    <%= f.text_field(:slug) %>
    <%= f.text_area(:body, :label => "Custom Label", :rows => 7, :cols => 25) %>
    <li class="buttons">
        <button name='commit[save]' type='submit' value='save'>Save</button>
        <button name='commit[cancel]' type='submit' value='cancel'>Cancel</button>
    </li>
<% end -%>


I’m planning on adding helpers to produce the buttons as well. However I’ve got to get over the small problem that IE doesn’t really like having multiple button tags as it doesn’t submit the correct value to the form.

The plugin is tested using Mathew Abonyi’s excellent plugin_test_kit.

Anyway, without further ado, the plugin can be checked out of subversion. or can be installed by ./script/plugin install http://svn.cumu.li/my_form_helper/

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in