Subscribe to RSS

RoR: link_to_remote with a text_field value as an argument

Forms are very well supported in Ruby on Rails. But in some cases you want to get information about a value a user has entered in a form, but without submitting the entire form. A good example of this is the sign up procedure at Yahoo. You enter your desired username, click “check availability” and you know if the name is available to you.

In my case I want to add locations to my database and use geocoding to get latituden/longitude values. So, I want users to enter an address and click “check” to verify that geocoding is successful. When successful, the full address and lat/lng values are automatically filled out in the form.

This article shows you how to update those text fields and most importantly, it shows how to add a text_field value as a paramater to your link_to_remote tag.

This last part is easy with RJS:

render :update do |page|
  page[:location_address].value = gecode.full_address
  page[:location_lat].value = geocode.lat
  page[:location_lng].value = geocode.lng
end

But now the tricky part! How to include the value of a text_field in a link_to_remote tag?? A normal link_to_remote tag might look like this:

link_to_remote "Check address",
   :url {
      :controller => 'locations',
      :action => 'check_address',
      :address => @location.address
   },
   :method => :get

The problem here is that we’re at the ‘new’ form, so @location doesn’t contain anything yet. Erb isn’t going to help us either because it’s already rendered and cannot change something ‘onClick’.

You got it, JavaScript is your friend! You could now write a JS function that retrieves the value of the text_field and add it to the link_to_remote request. Well, you could. But there is an easier way!

Prototype is the answer! It comes default with Rails and if you already use the javascript defaults, it’s already at your diposal. If not, include prototype in your layout first:

< %= javascript_include_tag 'prototype' %>

Now, we can use Form.element.getValue to get the text_field value. Since Prototype is very nice, there’s a shortcut available: $F(element).

The next question is how to integrate this with our link_to_remote tag. The :with paramater offers help!

link_to_remote "Check address",
:url {
:controller => 'locations',
:action => 'check_address',
:address => @location.address
},
:with => "'address='+encodeURIComponent($F(

Please share the love of this post by bookmarking it, and sharing it with others. Thanks!

  • Digg
  • del.icio.us
  • description
  • Reddit
  • Technorati
  • BlinkList
  • E-mail this story to a friend!
  • Facebook
  • Live
  • MisterWong
  • Netvouz
  • NewsVine
  • Slashdot
  • SphereIt

7 Comments

  1. Rushabh
    Posted 24 March, 2007 at 22:02 | Permalink

    I was looking for a way to pass some parameters to Redbox

    Thank You!!

  2. Posted 27 March, 2007 at 21:41 | Permalink

    This was incredibly helpful, thank you for the insight and thank you for taking the time to publish this. I will suggest that you make one change to your example.

    Change:

    $F(’spot_address’)

    To:

    encodeURIComponent($F(’spot_address’))

    If spot_address has any “funny” characters in it, it will break parsing. I had a different application where ampersands might be in the edit control, and it barfed out until I encoded them.

  3. Posted 27 March, 2007 at 21:46 | Permalink

    @Blake: you’re right about that. The article has been updated. Thanks for the comment!

  4. sathishkumar
    Posted 29 March, 2007 at 08:30 | Permalink

    thanks for comment
    its very usefull

  5. Phu V. Nguyen
    Posted 17 September, 2007 at 18:27 | Permalink

    I think you have some error in typing :D, so I rewrite it:
    link_to_remote “Check address”,
    :url => {
    :controller => ‘locations’,
    :action => ‘check_address’,
    :address => @location.address
    },
    :with => “‘address=’+encodeURIComponent($F(’spot_address’))”,
    :method => :get

    Cheer.

  6. Posted 6 August, 2008 at 07:45 | Permalink

    Does using RJS in the controller break the principles behind MVC?

  7. Soleone
    Posted 14 August, 2008 at 20:13 | Permalink

    @Personal Trainer:

    If you’re using it in the controller, I would say Yes. But for very simple stuff, that’s maybe not an issue.

    I tend to put all the RJS code in a View template (e.g. “action.js.rjs”) that gets rendered by the Controller.

Post a Comment

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

*
*