Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:19:45+00:00 2026-05-31T22:19:45+00:00

I have a Rails form for email, and I would like to have auto-completion

  • 0

I have a Rails form for email, and I would like to have auto-completion in Receiver field. Here is what I did:

= f.label :recipient_id, "Send to:"
= text_field_tag :recipient_id, nil
:javascript
 $(function() {
    $( "#recipient_id" ).autocomplete({
        source: "#{escape_javascript url_for(:action => 'recipient_autocomplete',
                     :controller => 'sent')}",
        minLength: 2,
        select: function( event, ui ) {
            $('input#recipient_id').attr('value', ui.item.value);
            ui.item.value = ui.item.label
        }
    });
});

In my controller, I have:

  def recipient_autocomplete
    @users= User.all(:conditions => ['first_name LIKE ? OR last_name LIKE?', 
      "%#{params[:term]}%", "%#{params[:term]}%"])
    render :json => @users.collect {|x| {:label=>x.full_name, :value=>x.id}}.compact
  end

The json call works fine and display full name OK. Here is the element:

<input type="text" name="recipient_id" id="recipient_id" class="ui-autocomplete-input"
  autocomplete="off" role="textbox" aria-autocomplete="list" 
  aria-haspopup="true" value="158">

However, when I click Send (equivalent to submit), the receipient full name is sent instead of receipient_id.

How do I tell Rails form to pick up the value (158 in the above example) of the input field instead of its label?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:19:46+00:00Added an answer on May 31, 2026 at 10:19 pm

    Err well the HTML spec says the text of the text_field is the value it’s going to return. But you can just use a hidden_field to pass the _id like so,

    = f.label :recipient_id, "Send to:"
    = text_field_tag :recipient_name, nil
    = hidden_field_tag :recipient_id, nil
    :javascript
     $(function() {
        $( "#recipient_id" ).autocomplete({
            source: "#{escape_javascript url_for(:action => 'recipient_autocomplete',
                         :controller => 'sent')}",
            minLength: 2,
            select: function( event, ui ) {
                $('input#recipient_name').val(ui.item.label);
                $('input#recipient_id').val(ui.item.value);
            }
        });
    });
    

    That’s from This JqueryUI page

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have Ajax form in Rails so i'm using form_remote_tag. The
I have a form, and for layout (and future use), i would like to
I am new to Rails. I have a User model. I would like a
Hi I have added active_admin to my rails app. I would like to create
I have a Rails form, where I am trying to insert a Select formfield
I have a simple Ruby on Rails form which includes an authenticity_token. Unfortunatly, I
We have the following code working for a complex rails form with checkboxes. I'm
I have some code to display a long list of languages inside Rails form
I'm trying to submit a Rails form with AJAX. I have some toggle on/off
I have a Rails app and trying to make a remote form, so far

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.