Skip to content
Extends Rails multiparameter assignment to actually work for Strings
Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
MIT-LICENSE
README
Rakefile
init.rb
install.rb
uninstall.rb

README

MultiparamsForStrings
=====================

Adds a condition to the ActiveRecord::Base#execute_callstack_for_multiparameter_attributes method to allow multiparam attribute assignment for strings.

The use case that inspired the plugin was trying to create a phone number field with 3 separate parts.

Installation
============

As with any plugin:

Rails 2.3.x:

  script/plugin install git://github.com/philcrissman/multiparams_for_strings.git
  
Rails 3.0.x:

  rails plugin install git://github.com/philcrissman/multiparams_for_strings.git

Caveat: At the time of writing this, the method in ActiveRecord::Base that this plugin overwrites is the same in Rails 2.3 and 3.0, so it should work the same in either version; if this stops working in Rails 3, please let me know.


Example
=======

No view helper is included in the plugin; you'll need to write your own, or write the input fields yourself. For example, your form might have:

 <input id="user_phone_1s" maxlength="3" name="user[phone(1s)]" size="3" type="text">
 <input id="user_phone_2s" maxlength="3" name="user[phone(2s)]" size="3" type="text">
 <input id="user_phone_3s" maxlength="4" name="user[phone(3s)]" size="4" type="text">

When you submit this, your params will include:

  :user => {"phone(1s)" => '123', "phone(2s)" => '456', "phone(3s)" => '7890', [etc...] }
  
...which ActiveRecord will be able to concatenate so that your finished user will have the phone number '1234567890'

  > User.last.phone
  => "1234567890"


Tests
=====

The tests can be run stand alone; cd to the plugin directory and just

  ruby test/multiparams_for_strings_test.rb
  
The tests do assume you have things like Rails and the sqlite3 gem installed.


Contributing
============

If you find a bug or think of a way to improve this, just let me know or submit a pull request. Thanks in advance.


Copyright (c) 2011 Phil Crissman, released under the MIT license
Something went wrong with that request. Please try again.