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 posted in JavaScript, Ruby on Rails. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

7 Comments

  1. Brent
    Posted November 2, 2006 at 11:16 am | Permalink

    What does the controller code looks like? Do you use request.raw_post || request.query_string ?

  2. KreeK
    Posted November 2, 2006 at 12:41 pm | Permalink

    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

  3. Carl
    Posted December 19, 2006 at 7:49 am | Permalink

    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

  4. Posted January 6, 2007 at 4:17 am | Permalink

    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

  5. Posted February 6, 2007 at 12:49 pm | Permalink

    @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”

  6. Posted June 26, 2008 at 5:45 am | Permalink

    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

  7. Posted June 26, 2008 at 6:17 am | Permalink

    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

  1. [...] javascript/prototype to get a text field value for [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word