Need remote_function help

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/
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

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”

On May 4, 2:23 am, elliottg [email protected] 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

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

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 "
}

%>

Thanks 7Stud!

That worked, nice and simple.

Elliott