Vixiom Axioms

October 19, 2006

Use javascript/prototype to get a text field value for link_to_remote

Filed under: JavaScript, Ruby on Rails Alastair @ 6:16 pm

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 :)

Digg! submit Use javascript/prototype to get a text field value for link_to_remote to stumbleupon.com submit Use javascript/prototype to get a text field value for link_to_remote to del.icio.us submit Use javascript/prototype to get a text field value for link_to_remote to reddit.com Like this post? subscribe to the feed.

7 Comments »

  1. 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

  2. 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

  3. 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’)”}
    User Columns (0.010000) SHOW FIELDS FROM users
    User Load (0.010000) SELECT * FROM users WHERE (users.`id` = 2 ) LIMIT 1
    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

  4. 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

  5. @Carl: I had the same problem. I had the :with-part in the :url-Hash.

    @Adrian: :with => “‘group%5Bname%5D=’+$F(’group_name’)+’&param2=foo&param3=bar”

    Comment by Robert Gerlach — February 6, 2007 @ 12:49 pm

  6. 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

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

RSS feed for comments on this post. TrackBack URL

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

Powered by WordPress