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.
The key line being
‘%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 :)




Like this post? subscribe to the feed.






What does the controller code looks like? Do you use request.raw_post || request.query_string ?
Comment by Brent — November 2, 2006 @ 11:16 am
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
Comment by KreeK — November 2, 2006 @ 12:41 pm
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
Comment by Carl — December 19, 2006 @ 7:49 am
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
Comment by Adrian Carr — January 6, 2007 @ 4:17 am
@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â€
Comment by Robert Gerlach — February 6, 2007 @ 12:49 pm
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
Comment by Gavin Laking — June 26, 2008 @ 5:45 am
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!)
Comment by Gavin Laking — June 26, 2008 @ 6:17 am