06 Mar 2009

“New in Rails 2.3 - Disabled option tags and lambdas for selecting and disabling options from collections”

As it seems to have slipped through the usual channels, I thought I’d document a couple of enhancements to the form option helpers here.

Disabled option tags

The form option helpers have been updated so that you can now specify disabled option tags:

<%= select(:post, :category, Post::CATEGORIES, :disabled => 'private') %>
 
<!— Give's you something like —>
 
<select name="post[category]">
  <option>story</option>
  <option>joke</option>
  <option>poem</option>
  <option disabled="disabled">private</option>
</select>

view raw This Gist brought to you by GitHub.

Disabled option tags appear greyed out and cannot be selected by the user (or at least they do in normal browsers – see below).

Select tag with disabled option tags

Lambdas for selecting and disabling option tags from collections

You can now use anonymous functions to specify which members of a collection should be selected or disabled in the resultant option tags. For example, you may want to disable product sizes that are out of stock:

<%= options_from_collection_for_select(@product.sizes, :name, :id, :disabled => lambda{|size| size.out_of_stock?}) %>
 
<!— If small and medium returned true for the method out_of_stock?, you’d get something like: —>
 
<option value=“23“ disabled=“disabled“>small</option>
<option value=“24“ disabled=“disabled“>medium</option>
<option value=“25“>large</option>
<option value=“26“>extra large</option>

view raw This Gist brought to you by GitHub.

This works exactly the same for selecting values, simply pass your anonymous function in as :selected.

A word of caution – always coding defensively

Although disabled option tags are part of the HTML specification, Internet Explorer 6 and 7 do not support them and will display disabled option tags as selectable. And although there are hacks to get IE to behave, you can never guarantee that disabled values will not be submitted by someone constructing a html post. Therefor, it’s always best to code defensively and guard against the potential selection of disabled values in your controller actions.

Posted in Rails , Plugins | Comments not ready yet..

05 Jan 2009

“Disabled option tags in rails forms”

Update: My patch has now been accepted into core, which means that as of Rails 2.3, this plugin should no longer be necessary. More details here.

Ever wanted to disable options in a form select in rails? I have; it feels more useable to me to disable out of stock product sizes:

Select with a disabled option tag greyed out and not selectable

The rails Form Option Helpers won’t let you do this as they currently stand. So until things change in core, I’ve put together the option_tags_will_disable plugin.

Simply specify the disabled values when calling options_for_select:

<%= options_for_select(
  ['Choose a size', 'small', 'medium', 'large'],
  nil, # selected value
  'medium' # disabled value
) %>
 
<!— Gives you —>
 
<option value="Choose a size">Please choose a size</option>
<option value="s">small</option>
<option value="m" disabled="disabled">medium</option>
<option value="l">large</option>
 

view raw This Gist brought to you by GitHub.

You can do the same when working with collections, but more interestingly, you can also give a Proc to identify which elements should be disabled:

<%= options_from_collection_for_select(
  @products,
  :id,
  :name,
  nil, # selected value(s)
  lambda{|p| !p.in_stock? } # disabled value(s) identified with a proc
) %>
 

view raw This Gist brought to you by GitHub.

And as an added bonus, selected options can also be identified with a Proc!

Posted in Rails , Plugins | Comments not ready yet..

26 Nov 2008

“Rails gets some engines love”

No sooner is Rails 2.2 out the door than edge Rails gets exciting again!

A tasty little commit hints at some major changes to the way Rails handles plugins. Finally, the awesome features of the engines plugin (lovingly crafted by James Adams) will be creeping into core.

I’m not one to speculate what brought about the change in heart, I’m just glad Rails is finally drinking the engines Kool-Aid!

Engines? Wha?

As it says in the README: “The engines plugin enhances Rails’ own plugin framework, making it simple to share controllers, helpers, models, public, assets, routes and migrations in plugins”.

In a nutshell, you get to create slices of apps as plugins which makes it a piece of cake to to share common functionality between multiple applications. In my case, I’ve been able to put together several e-commerce websites with practically all the core functionality provided by a set of plugins. The beauty of it is, you can easily override views, actions, etc, in your app to make each site it’s own. And when you fix a bug, or add a new feature, all the sites can benefit!

What comes next?

It’s still early days and it’s not yet clear just how much of this is getting rolled into Rails, but James has hinted that much will make it across, including migrations, routes and asset management.

In the mean time, if you want to see what all the fuss is about, check out the engines plugin which, thanks to the hard work of everyone involved, now runs happily on Rails 2.2!

Update

The commits are coming thick and fast now. I’m certainly looking forward to road testing these new feature as they take shape.

Posted in Rails , Plugins | Comments not ready yet..

View older posts »