[Ruby] JavaScript in FormHelpers

Tom Lianza tlianza at gmail.com
Mon Nov 20 11:45:22 PST 2006


On 11/17/06, Neil Moomey <neil at motznik.com> wrote:
>
> How do I add JavaScript to FormHelper tags?  For example in a radio button
> I
> want to display something like :
>
> <input value="Address" name="searchby" type="radio" id="Address"
> onClick="javascript:redirect('QueryA1R1.rhtml')" />
>
> but I want to use a FormHelper like:
> <%= radio_button "post", "category", "rails" %>


The docs always seem to be a little terse on this topic, by just listing a
parameter called "options" and hoping people figure it out from there:
http://rubyonrails.org/api/classes/ActionView/Helpers/FormHelper.html#M000395

I believe to get what you want you'd do something like:
<%= radio_button("post", "category", "rails",
{:onclick=>"javascript:redirect('QueryA1R1.rhtml')"})%>


Also, why do Form helpers always output arrays for the name attribute?
> name=post[category]


This becomes hugely convenient when you're  using the form submission to
update attributes of an object.  For example, if you're working with a
"Post" object, you can then do something like:

mypost.update_attributes(params[:post])

It can then collect all of the attributes out of the "post" hash (sent with
the form submission) without you having to write any glue code to map
them/escape them/list them one by one/etc.

The "Agile Web Development with Rails" book does a good job of explaining
the benefits of these conventions.

Thanks
>
> Neil
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby
>


More information about the Ruby mailing list