开发者

In Rails 3, I'm using the jQuery Tokenize plugin but the form submits the value, not the id

I love this plugin, I've used this in PHP with ease, but I'm trying now in Rails 3 and for some reason when the form gets submitted, it's not submitting the id of the selected item, it's submitting the actual value. I've even printed out the JSON to make sure that the data is being fed to the input field properly and it's showing the ID and the VALUE.

Any ideas on why this is happening. I've been thinking that maybe I need to add a custom javascript call for the onBlur, but I've never had to do that prior to this. Anyone have any other suggestions, or even a totally different way to accomplish an autocomplete in Rails with multiple selections?

I appreciate any help that you can give me, thanks!! And yes, I know the input isn't cleaned :)

  def autocomplete
    autolist = []
    sql = "SELECT id,us开发者_如何学编程ername from users WHERE username LIKE '" + params[:q] + "%'"
    result = ActiveRecord::Base.connection.execute(sql)
    result.each { |field| autolist.push( {"id" => field[0], "name" => field[1]} ) }
    render :json => autolist, :layout => false
  end

The code for the form:

<% form_for @message, :url => user_messages_path(@user), :html => { :multipart => true } do |f| %>
    <tr>
        <% if @reply_to.nil? %>
        <td width="100"><p class="labels">To:</p></td>
        <td><%= f.text_field :to %>
            <%= error_message_on @message, :to %></td>
        <% else %>
      <td width="100">To: <%= @message.to %><%= f.hidden_field :to, :value => @message.to  %></td>
        <% end %>
    </tr>
  <tr>
    <% if @reply_to.nil? %>
    <td width="100"><p class="labels">Patient:</p></td>
    <td><%= f.collection_select :patient_id, Patient.find_all_by_user_id(current_user), :id, :last_name %>
        <%= error_message_on @message, :patient_id %></td>
    <% else %>
    <td width="100">Patient: <%= Patient.find(@message.patient_id).last_name %><%= f.hidden_field :patient_id, :value => @message.patient_id  %></td>
    <% end %>
  </tr>
  <tr>
    <% if @reply_to.nil? %>
    <td width="100"><p class="labels">Subject:</p></td>
    <td><%= f.text_field :subject %>
    <%= error_message_on @message, :subject %></td>
    <% else %>
    <td width="100">Subject: <%= @message.subject %><%= f.hidden_field :subject, :value => @message.subject  %></td>
    <% end %>
  </tr>
  <tr>
      <td width="100" valign="top"><p class="labels">Message:</p></td>
      <td><%= f.text_area :body %>
            <%= error_message_on @message, :body %></td>
  </tr>
  <tr>
      <td width="100" valign="top"><p class="labels">Attachment:</p></td>
      <td><%= f.file_field :attachment %><%= error_message_on @message, :attachment %></td>
  </tr>
  <tr>
      <td colspan="2"><%= submit_tag "Send" %></td>
  </tr>
</table>

Javascript

<script language="javascript">
$(function($) {
    $("#message_to").tokenInput("/users/1/friends/autocomplete", {
                hintText: "Type in the name of a colleague",
                noResultsText: "No results",
                searchingText: "Searching..."
            });
});
</script>


Your text field and your hidden field need different names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜