Use javascript/prototype to get a text field value for link_to_remote
This one was a headache, and by headache I mean I was literally pounding my head on the desk trying to figure it out.
I wanted to use link_to_remote to create a model from another model’s form. The example being a ‘Product’ model that belongs to a ‘Group’, the user would select which group a product belonged to within the Product form. Within being the key word because you can’t use ‘remote_form_for’ because it will submit the ‘master’ form.
The solution I came up with was to use link_to_remote instead, but how to pass a text_field’s value to link_to_remote’s arguments. After hacking away I gave up and shot an email off to the AHG Software folks who have worked with me on a couple of Rails projects.
The answer was to use prototype’s $F() function which returns the value of any field input control.
<div id="group">
<ul id="group_list">
<%= render :partial => 'groups/group_list', :collection => @groups %>
</ul>
<% @group = Group.new %>
<%= text_field 'group','name' %><br />
<%= link_to_remote "Add Group",
{ :update => "group_list",
:url => { :controller => "groups", :action => "new"},
:with => "'group%5Bname%5D='+$F('group_name')",
:position => :bottom },
{ :class => "add" } %>
</div>
The key line being
:with => "'group%5Bname%5D='+$F('group_name')",
‘%5B’ and ‘%5D’ are opening and closing square brackets [], which become more familiar to Rubyists (Rubians?… San Diegons?) as ‘group[name]‘.
Problem solved! – with a little help
This entry was written by
Alastair, posted on
October 19, 2006 at 6:16 pm, filed under
JavaScript,
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.
7 Comments
What does the controller code looks like? Do you use request.raw_post || request.query_string ?
the information comes across in the params, so the new method for the above example would be
def new
@group = Group.new
@group.name = params[:group][:name]
@group.save
render(:partial => ‘/groups/group’)
end
Hello, It does not seem to work. here some logs:
Processing ShopAdminController#get_full_address (for 127.0.0.1 at 2006-12-19 14:35:54) [POST]
Session ID: 8e638876f4cdef90e7b9ac74bbf6160b
Parameters: {”action”=>”get_full_address”, “id”=>”$F(’country’)”, “controller”=>”shop_admin”, “with”=>”’shop%5Bpostcode%5D=’+$F(’shop_postcode’)”}
[4;36;1mUser Columns (0.010000)[0m [0;1mSHOW FIELDS FROM users[0m
[4;35;1mUser Load (0.010000)[0m [0mSELECT * FROM users WHERE (users.`id` = 2 ) LIMIT 1[0m
Completed in 0.04000 (25 reqs/sec) | Rendering: 0.00000 (0%) | DB: 0.02000 (50%) | 200 OK [http://localhost/shop_admin/get_full_address/%24F%28%27country%27%29?with=%27shop%255Bpostcode%255D%3D%27%2B%24F%28%27shop_postcode%27%29]
shop_postcode should have returned the value. It looks like the new helper escapes the javascript….
Any ideas. If I put an alert for $F(’shop_postcode’), I get the value entered….
/Carl
Hi. I was wondering if there is a way to send multiple parameters with link_to_remote? I’ve been searching and trying, to no avail.
Thanks,
Adrian
@Carl: I had the same problem. I had the :with-part in the :url-Hash.
@Adrian: :with => “‘group%5Bname%5D=’+$F(’group_name’)+’¶m2=foo¶m3=barâ€
I’ve posted here since I used this page as the basis to get the first parameter in there; someone else Googling will hopefully reach here too. If you want to put multiple parameters in there; it’s simple once you know how (but I took 15 minutes to get this working properly!):
link_to_remote ‘Check Availability’, {:update => “check”, :url => {:controller => “availability”, :action => “check”}, :with => “‘booking[date]=’ + $F(’booking_date’) + ‘booking[centre_id]=’ + $F(’booking_centre_id’)”}
The magic is once again in the :with, but convention leads you to believe that HTTP parameters should be passed with an ampersand, in Rails though it’s the concatenation symbol + (plus).
Hope that helps someone.
Gav
I’m an idiot. You need to add another ampersand to the begining of booking[centre_id] like this: ‘&booking[centre_id]=’
Sorry about that. (That’s what comes of not testing my code!)
One Trackback
[...] javascript/prototype to get a text field value for [...]