Rails 2.0 and link_to_remote :with
I’m moving an old app to Rails 2.0 and other than fixing some routes Ajax calls with link_to_remote and the :with parameter was the only thing that gave me some trouble. In the old version I was grabbing the current value of a drop down list with Prototype and passing it along using :with like this
<%= link_to_remote image_tag('cms/add_16.gif', :id => "color_add"),
:url => { :controller => "colors", :action => "new_ajax",
:id => @product.id },
:with => "{ color_id: $F('color_id') }"
%>
But in Rails 2.0 the parameter wouldn’t go along for the ride, it seemed that the new authenticity_token that gets sent with Ajax calls was messing things up. Here’s the fix
<%= link_to_remote image_tag('cms/add_16.gif', :id => "color_add"),
:url => { :controller => "colors", :action => "new_ajax",
:id => @product.id },
:with => "'color_id='+$F('color_id')"
%>
I freely admit JavaScript/Ajax is my weakest language so if I was doing it wrong the entire time let me know
This entry was written by
Alastair, posted on
January 18, 2008 at 11:22 am, filed under
Ajax,
JavaScript,
Ruby,
Ruby on Rails. Bookmark the
permalink. Follow any comments here with the
RSS feed for this post.
or leave a trackback:
Trackback URL.
© Copyright 2006 - 2012 Alastair Dawson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 Comments
thanks! I am new to rails in general and had no clue how to attach a dom node value to my ajax requests. This is kind of a cornerstone of what I like to do.
Now I know!
that’s work for me! thanks!
“0″, :align=>”absmiddle”), :update => “resultado”, :url => { :action => “actualiza_lineas” }, :with => “{ filtro_lineas: $F(’filtro_lineas’) }” %>
a question… what’s global var $F ?? the form id, name or so??
$F is a protoype helper function to query the value of the form-input element given as id in the parameters of the $F function.