# Pass multiple form element values with link\_to\_remote?

**URL:** <https://www.ruby-forum.com/t/pass-multiple-form-element-values-with-link-to-remote/125787>\
**Category:** Rails\
**Created:** [January 12, 2008, 1:21am UTC](https://www.ruby-forum.com/t/pass-multiple-form-element-values-with-link-to-remote/125787 "2008-01-12T01:21:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 12, 2008, 1:21am UTC](https://www.ruby-forum.com/t/pass-multiple-form-element-values-with-link-to-remote/125787/1 "2008-01-12T01:21:10Z")

</div>

Let me preface this by noting that I’m new to Rails and hardly know  
anything about Javascript…hence why I need help. 🙂

That said, I have a form which contains three text fields: site\_id,  
start\_date, and end\_date. I have a link\_to\_remote link which needs to  
pass these three values to an action when I click it, but I can’t for  
the life of me figure out how to access / pass the three text field  
values on so that I can use them.

Any help for me?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 12, 2008, 1:32pm UTC](https://www.ruby-forum.com/t/pass-multiple-form-element-values-with-link-to-remote/125787/2 "2008-01-12T13:32:34Z")

</div>

you probably should make it a link\_to\_function instead which calls  
form.submit() (where form is the id of your form). Then turn your form  
into a remote\_form\_tag or remote\_form\_for. I think this would work  
because you are triggering the onsubmit handler of the form which will  
contain the AJAX request.

e.g. (approximately)

\<%= link\_to\_function(‘save’,‘my\_form.submit();’) %\>  
\<%= image\_tag(‘/images/spinner.gif’, :alt =\> ‘Loading’), :id =\>  
‘spinner’ %\>  
\<%= form\_remote\_tag(:url =\> {:controller =\> ‘sites’, :action =\>  
‘create’}, :before =\> “Element.show(‘spinner’)”, :after =\>  
“Element.hide(‘spinner’)”, :html =\> {:id =\> ‘my\_form’}) do %\>  
\<%= text\_field\_tag(:site\_id) %\>  
\<%= text\_field\_tag(:start\_date) %\>  
\<%= text\_field\_tag(:end\_date) %\>  
\<% end %\>

Not too familiar with the exact syntax for form\_remote\_tag, but it is  
along those lines. Try [gotapi.com/rubyrails](http://gotapi.com/rubyrails) for the full reference.  
I’ve added the image there for a loading spinner but you could equally  
write some text or whatever, just make sure there is an element with  
id=‘spinner’, or change the :before and :after calls (or remove them).

You can also use remote\_form\_for if you are changing a model directly.  
I prefer this way. Finally, you can use restful or named routes in  
place of the {:controller =\> … :action =\> …} hash

If you want to know more about selecting elements from the DOM using  
prototype’s $ helpers check that out on the prototype webiste  
[http://www.prototypejs.org/](http://www.prototypejs.org/)

Hope this assists.

Joe

On Jan 12, 12:20 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 12, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/pass-multiple-form-element-values-with-link-to-remote/125787/3 "2008-01-12T19:46:35Z")

</div>

I actually got what I wanted using the link\_to\_remote. I’m not sure  
the form\_remote\_tag was exactly what I wanted, but I appreciate the  
help because I will certainly need that later on with what I’m doing.  
The solution was this:

In my view:

\<%= link\_to\_remote("Calculate Invoice", :url =\> { :action =\> :calculate\_invoice }, :method =\> 'get', :with =\> 'serialize\_fields()', :update =\> 'invoice-div' )%\>

## Invoice Amount

Pending selection...

In application.js: (code stolen from another post in the group)

function serialize\_fields(){  
var fields = new Array();  
fields.push(Form.Element.serialize(‘reservation\_site\_id’));  
fields.push(Form.Element.serialize(‘reservation\_start\_date’));  
fields.push(Form.Element.serialize(‘reservation\_end\_date’));  
return fields.join(’&’);  
}

In my controller (this is ugly as sin and there are probably a ton of  
better ways to do this:

def calculate\_invoice  
@site = Site.find(params[:reservation][:site\_id].to\_s)  
nightly\_rate = @site.default\_rate  
start\_date = params[:reservation][:start\_date].to\_date  
end\_date = params[:reservation][:end\_date].to\_date  
total\_nights = end\_date - start\_date  
invoice\_total = nightly\_rate \* total\_nights  
render :partial =\> ‘invoice’, :locals =\> { :nightly\_rate =\>  
nightly\_rate.to\_s, :total\_nights =\> total\_nights.to\_s, :invoice\_total  
=\> invoice\_total.to\_s }  
end

And then my \_invoice.rhtml:

| Total Nights | Nightly Rate | Invoice Total |
| --- | --- | --- |
| \<%= total\_nights %\> | \<%= nightly\_rate %\> | \<%= invoice\_total %\> |

Anybody feel free to advise me where I could have done things better  
because I’m still very very new to coding in general, and especially  
new to Rails.

Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 12, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/pass-multiple-form-element-values-with-link-to-remote/125787/4 "2008-01-12T20:54:05Z")

</div>

Oops, I mixed up information. Really, what you wanted to use was a  
remote\_form, though, as wildtangent suggested.

Use a form for multiple values that are entered on the page after the  
page loads.  
Use a link/button for any action using params that were already  
available from the controller action.

On Jan 12, 1:46 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 13, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/pass-multiple-form-element-values-with-link-to-remote/125787/5 "2008-01-13T14:11:38Z")

</div>

Just reading this thread made me wonder. I thought serialize was a  
standard function.

Having looked, and, yes serialize is in the Scriptaculous library, so  
you don’t need to write the function.

Of course you may have written your own because you only want to  
include a subset of the fields.

Also noticed this thread which included a subset function - which you  
may already have seen.

[http://groups.google.ca/group/rubyonrails-talk/msg/3df638d6c271d8c0](http://groups.google.ca/group/rubyonrails-talk/msg/3df638d6c271d8c0)

Tonypm

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 12, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/pass-multiple-form-element-values-with-link-to-remote/125787/6 "2008-01-12T20:47:01Z")

</div>

You can pass any params you wish in the :url hash

In this case:  
\<%= link\_to\_remote(“Calculate Invoice”,  
:url =\> { :action =\> :calculate\_invoice, :start\_date =\>  
params[:reservation][:start\_date], :end\_date =\> params[:reservation]  
[:end\_date] },  
:method =\> ‘get’,  
:update =\> ‘invoice-div’ )%\>

Then find total\_nights in the calculate\_invoice controller action.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 11, 2009, 11:35am UTC](https://www.ruby-forum.com/t/pass-multiple-form-element-values-with-link-to-remote/125787/7 "2009-10-11T11:35:25Z")

</div>

thanks, It helped me. My problem solved.
