text_field_with_auto_complete pass multiple field values

In ruby on rails there is a method called text_field_with_auto_complete. By default it will call a method on your controller with the name auto_complete_for_<form>_<field>. It will pass in a param value for the field you are updating. The details of this I am not going into here, they are well documented elsewhere.

For this exercise I am going to show how to setup the tag so that you can pass multiple field values.

Example:


<%=  text_field_with_auto_complete :user, 
    :habitat_name, 
    {:class => 'input'}, 
    {:with => "'&user[state]=' + jQuery('#user_state').val() + 
        '&user[habitat_name]=' + jQuery('#user_habitat_name').val()", 
     :skip_style => false} %>

In the example above I am using the jrails plugin to implement jQuery so the syntax is for jQuery. The key clause is the :with class. Here you will need to implement not only the primary field you want to pass, but can add any others you wish. In my case I have a user[:habitat_name] and user[:state]

This entry was posted in Ruby on Rails. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree