# Need remote\_function help

**URL:** <https://www.ruby-forum.com/t/need-remote-function-help/167103>\
**Category:** Rails\
**Created:** [May 4, 2009, 2:26am UTC](https://www.ruby-forum.com/t/need-remote-function-help/167103 "2009-05-04T02:26:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [May 4, 2009, 2:26am UTC](https://www.ruby-forum.com/t/need-remote-function-help/167103/1 "2009-05-04T02:26:34Z")

</div>

This is the first time I have used remote\_function and I’m unclear on  
why my code is not working as I would expect.

I am trying to have my select box load the specified action and pass a  
few params to it when an item is selected.

Here’s an outtake of the code I have:

\<% form\_for(@order) do |f| %\>  
\<%= select(:state, :state, [[‘Alabama’, ‘AL’], … [‘Wyoming’,  
‘WY’]], {},  
html\_options = {:onchange =\> remote\_function(:url =\> {:action =\>  
“new”, :id =\> 74}, :with =\> “‘state\_test=’+this.value”, :method  
=\> :get)} ) %\>  
\<% end %\>

I want it to call OrdersController#show, and according to my dev log  
it actually is, and it’s passing all the desired params as well.  
Here’s a log entry:

Processing OrdersController#show (for 127.0.0.1 at 2009-05-03  
20:11:13) [GET]  
Session ID: a612ff378ef73097a30185e62e9ab64b  
Parameters: {“state\_test”=\>“NY”, “action”=\>“show”,  
“authenticity\_token”=\>“b3ad00a2fdc2c467fcea159623d20470cfa54f8c”,  
“id”=\>“74”, “controller”=\>“orders”}  
Order Columns (47.0ms) SHOW FIELDS FROM `orders`  
Order Load (81.0ms) SELECT \* FROM `orders` WHERE (`orders`.`id` =  
74)  
Rendering template within layouts/orders  
Rendering orders/show  
Completed in 141ms (View: 7, DB: 129) | 200 OK [[http://localhost/](http://localhost/)  
orders/74?  
state\_test=NY&authenticity\_token=b3ad00a2fdc2c467fcea159623d20470cfa54f8c]

According to the log this thing should be working I’d think. But no  
matter what action I target the view never refreshes or is changed.

What am I doing wrong?

Thanks, Elliott

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [May 4, 2009, 3:16am UTC](https://www.ruby-forum.com/t/need-remote-function-help/167103/2 "2009-05-04T03:16:42Z")

</div>

Elliott G. wrote:

> What am I doing wrong?

The definition for remote\_function I’m looking at has an argument:

:update =\> “id of element to update”

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [May 4, 2009, 9:43am UTC](https://www.ruby-forum.com/t/need-remote-function-help/167103/3 "2009-05-04T09:43:08Z")

</div>

On May 4, 2:23 am, elliottg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Since I am not rendering an rjs template what would this be? I just  
> want the show.html.erb template to draw out in its entirety.

you need to pass the id of a dom element where the render results will  
be placed.

Fred

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [May 4, 2009, 3:24am UTC](https://www.ruby-forum.com/t/need-remote-function-help/167103/4 "2009-05-04T03:24:45Z")

</div>

Since I am not rendering an rjs template what would this be? I just  
want the show.html.erb template to draw out in its entirety.

Thanks

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [May 4, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/need-remote-function-help/167103/5 "2009-05-04T13:43:35Z")

</div>

Elliott G. wrote:

> Since I am not rendering an rjs template what would this be? I just  
> want the show.html.erb template to draw out in its entirety.
> 
> Thanks

\<%= select(:state, :state, [[‘Alabama’, ‘AL’], … [‘Wyoming’,  
‘WY’]], {},  
html\_options = {:onchange =\> remote\_function(:url =\> {:action =\>  
“new”, :id =\> 74}, :with =\> “‘state\_test=’+this.value”, :method  
=\> :get)} )  
%\>

This is the first time I’ve seen the remote\_function method but this is  
what I think is happening. Your code creates an html tag. The  
html\_options variable is used to write attributes into the tag,  
e.g.

So your code adds an onchange attribute to the tag, which calls  
a javascript function. You’ve also given the particulars for the  
javascript function to construct a request to send to the server when  
the javascript function gets called. Then the server will send the  
results back to the javascript function. So you need to tell the  
javascript function where you want the results to be inserted on the  
page. Javascript doesn’t automatically do anything with the results.  
That is what the :update parameter for remote\_function does–it tells  
the javascript function what to do with the results, i.e. insert the  
results in the specified tag.

> I just want the show.html.erb template to draw out in its entirety.

So you want to replace the whole page? What if you specified the id of  
the tag? If all the layout stuff gets added to the template, I  
don’t think that will work–you’d end up with another DOCTYPE tag, etc.  
inside the tag making your html a mess.

In addition, when you need to replace the whole page, you don’t use  
javascript to make an ajax request to the server. It sounds like you  
need something like the following inside your tag:

I’m a beginner, so I don’t know if rails can do that for you. You might  
have to write that by hand doing something like this:

\<%= select(:state, :state, [[‘Alabama’, ‘AL’], … [‘Wyoming’,  
‘WY’]], {},

html\_options =  
{  
:onchange =\> "window.location.href = ‘/show?id=74&state\_test=’ +  
this.value "  
}

%\>

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [May 4, 2009, 8:39pm UTC](https://www.ruby-forum.com/t/need-remote-function-help/167103/6 "2009-05-04T20:39:42Z")

</div>

Thanks 7Stud!

That worked, nice and simple.

Elliott
