Forum: Ruby on Rails Need remote_function help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7bd5cba5820168467074439c6a03c76b?d=identicon&s=25 Elliott Golden (elliottg)
on 2009-05-04 02:26
(Received via mailing list)
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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-05-04 03:16
Elliott Golden wrote:
> What am I doing wrong?
>


The definition for remote_function I'm looking at has an argument:

:update => "id of element to update"
7bd5cba5820168467074439c6a03c76b?d=identicon&s=25 Elliott Golden (elliottg)
on 2009-05-04 03:24
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-04 09:43
(Received via mailing list)
On May 4, 2:23 am, elliottg <x...@simplecircle.net> 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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-05-04 13:43
Elliott Golden 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 <select> tag.  The
html_options variable is used to write attributes into the <select> tag,
e.g.

<select id="hello" class="cool_css" onchange="javascript function call">

So your code adds an onchange attribute to the <select> 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 <body> 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 <body> 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 <select> tag:

<select onchange="window.location.href = '/show?id=74&state_test=' +
this.value ">

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

%>
7bd5cba5820168467074439c6a03c76b?d=identicon&s=25 Elliott Golden (elliottg)
on 2009-05-04 20:39
(Received via mailing list)
Thanks 7Stud!

That worked, nice and simple.

Elliott
This topic is locked and can not be replied to.