# ? Buttonless?

**URL:** <https://www.ruby-forum.com/t/buttonless/175904>\
**Category:** Rails\
**Created:** [September 22, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/buttonless/175904 "2009-09-22T16:44:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 22, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/buttonless/175904/1 "2009-09-22T16:44:25Z")

</div>

I have this code, which is working quite well:

\<% form\_tag ‘’, :method =\> ‘GET’ do %\>

\<%= radio\_button\_tag :report, 'by\_date', params[:report] ||= 'by\_date' %\> **By Date**

\<%= radio\_button\_tag :report, 'by\_donor', params[:report] == 'by\_donor' %\> **By Donor**

\<%= submit\_tag "Choose" %\>

\<% end %\>

\<%= render( :partial =\> “orders” , :object =\> @orders ) %\>  
(Inside of the \_orders partial is where params[:report] is  
processed)

But it’s ugly in that pressing “Choose” is redundant–I want the  
partial action to happen when the user simply selects the radio  
button. Is there any way to do this?

Many TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 22, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/buttonless/175904/2 "2009-09-22T16:54:15Z")

</div>

Dudebot wrote:

> I have this code, which is working quite well:
> 
> \<% form\_tag ‘’, :method =\> ‘GET’ do %\>
> 
> \<%= radio\_button\_tag :report, 'by\_date', params[:report] ||= 'by\_date' %\> **By Date**
> 
> \<%= radio\_button\_tag :report, 'by\_donor', params[:report] == 'by\_donor' %\> **By Donor**
> 
> \<%= submit\_tag "Choose" %\>
> 
> \<% end %\>
> 
> \<%= render( :partial =\> “orders” , :object =\> @orders ) %\>  
> (Inside of the \_orders partial is where params[:report] is  
> processed)
> 
> But it’s ugly in that pressing “Choose” is redundant–I want the  
> partial action to happen when the user simply selects the radio  
> button. Is there any way to do this?

Sure. There’s no need for a form here as far as I can see – just use  
links for the two options.

> Many TIA,  
> Craig

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 22, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/buttonless/175904/3 "2009-09-22T18:37:59Z")

</div>

Hi Craig,

On Mon, 2009-09-21 at 19:56 -0700, Dudebot wrote:

> \<%= render( :partial =\> “orders” , :object =\> @orders ) %\>  
> (Inside of the \_orders partial is where params[:report] is  
> processed)
> 
> But it’s ugly in that pressing “Choose” is redundant–I want the  
> partial action to happen when the user simply selects the radio  
> button. Is there any way to do this?

Assuming you’re OK with an Ajax request, check out the docs for  
observe\_form.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 22, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/buttonless/175904/4 "2009-09-22T17:35:39Z")

</div>

Or look up:  
[http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002477&name=observe\_form](http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002477&name=observe_form)  
[http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002476&name=observe\_field](http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002476&name=observe_field)

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 22, 2009, 11:42pm UTC](https://www.ruby-forum.com/t/buttonless/175904/5 "2009-09-22T23:42:29Z")

</div>

Dudebot wrote:

> I have this code, which is working quite well:
> 
> \<% form\_tag ‘’, :method =\> ‘GET’ do %\>
> 
> \<%= radio\_button\_tag :report, 'by\_date', params[:report] ||= 'by\_date' %\> **By Date**
> 
> \<%= radio\_button\_tag :report, 'by\_donor', params[:report] == 'by\_donor' %\> **By Donor**
> 
> \<%= submit\_tag "Choose" %\>
> 
> \<% end %\>
> 
> \<%= render( :partial =\> “orders” , :object =\> @orders ) %\>  
> (Inside of the \_orders partial is where params[:report] is  
> processed)
> 
> But it’s ugly in that pressing “Choose” is redundant–I want the  
> partial action to happen when the user simply selects the radio  
> button. Is there any way to do this?
> 
> Many TIA,  
> Craig

The Radio button helper also has an onclick option so you can remote  
call a function when ever they click the button, or execute some  
javaScript. I have done this many times in my apps, for exmaple with  
gender:

\<% func1 = remote\_function( :update =\> ‘’, :url =\> {  
:action =\> ‘gender\_female’, :id =\> @user.id},  
:with =\> “‘select=’ + escape(value)” ) %\>  
\<% func2 = remote\_function( :update =\> ‘’, :url =\> {  
:action =\> ‘gender\_male’, :id=\> @user.id},  
:with =\> “‘select=’ + escape(value)” ) %\>  
Male:  
\<%= radio\_button\_tag( :gender, :male,  
 @user.gender.to\_s == “Male” ? true : false,  
:onclick =\> func2 )%\>  
Female:  
\<%= radio\_button\_tag( :gender, :female,  
 @user.gender.to\_s == “Female” ? true : false,  
:onclick =\> func1 )%\>

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 23, 2009, 2:37am UTC](https://www.ruby-forum.com/t/buttonless/175904/6 "2009-09-23T02:37:01Z")

</div>

Thanks, Bill! The docs for observe\_form are sparse:

"Observes the form with the DOM ID specified by form\_id and calls a  
callback when its contents have changed. The default callback is an  
Ajax call. By default all fields of the observed field are sent as  
parameters with the Ajax call.

The options for observe\_form are the same as the options for  
observe\_field. The JavaScript variable value available to the :with  
option is set to the serialized form by default."

…

observe\_field seems quite different… I’ve looked up various  
references on the web and played around without success. How would I  
use “observe\_form” with my code? I get the feeling the answer is  
simple, and I’m just missing it…

Many TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 23, 2009, 3:12am UTC](https://www.ruby-forum.com/t/buttonless/175904/7 "2009-09-23T03:12:10Z")

</div>

This for example does not work: (index.html.erb)  
…  
\<% form\_tag ‘’, { :id =\> ‘myform’, :method =\> :get } do %\>

\<%= radio\_button\_tag :report, 'by\_date', params[:report] ||= 'by\_date' %\> **By Date**

\<%= radio\_button\_tag :report, 'by\_donor', params[:report] == 'by\_donor' %\> **By Donor**

\<%= submit\_tag "Choose" %\>

\<% end %\>

\<%= observe\_form( ‘myform’,  
:frequency =\> 1,  
:with =\> “report”,  
:action =\> :index ) %\>

\<%= render( :partial =\> “reports” ) %\>  
…

Any ideas?  
Again, many TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 23, 2009, 3:27am UTC](https://www.ruby-forum.com/t/buttonless/175904/8 "2009-09-23T03:27:59Z")

</div>

Hi Craig,

On Mon, 2009-09-21 at 20:57 -0700, Dudebot wrote:

> This for example does not work: (index.html.erb)

What, exactly, do you mean ‘does not work’? Is the Ajax request not  
firing? Are there no params? If there are params, are they not named  
what you expect?

If you’re going to use/debug Ajax, you need to be using Firefox and the  
Firebug plugin. You will be able to see all requests, params, and  
responses in the Firebug NET tab, and all Ajax requests, etc. in the  
Console tab.

To help further, I’ll need to know what you’re seeing.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 23, 2009, 3:18am UTC](https://www.ruby-forum.com/t/buttonless/175904/9 "2009-09-23T03:18:36Z")

</div>

This gives you an ajax response.  
\<%= radio\_button\_tag :report, ‘by\_date’, params[:report] ||=  
‘by\_date’, :onclick =\> remote\_function( :url=\> { :controller =\>  
‘myforms’, :action =\> ‘do’ } ) %\>
