Search code examples
ruby-on-railsruby-on-rails-3jquery-pluginsjquery-tokeninput

jquery token input plugin not passing the id from the select list


I am working on a simple rails plugin where a user can select a form containing the list of school available in the database. My problem is that the list of schools is being fetched but if I select the rest of the schools in the array I get the first item's id. What could I be doing wrong? My code sample is as follows

my user model has

belongs_to :school

  attr_reader :school_tokens

  def school_tokens=(id)
    self.school_id = id.split(",")
  end 

then in my school model I have

has_many :users

in my form I have

<%= f.label :school, "School Name" %>
<%= f.text_field :school_tokens %>

my application.js looks like

$(function(){
  $('#user_school_tokens').tokenInput("/school_streets.json", {
      crossDomain: false,
      tokenLimit: 1
  });

finally my schools controller is like this

  def index
    @school_streets = SchoolStreet.where("name LIKE ?", "%#{params[:q]}%")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @school_streets.map(&:attributes) }
    end
  end

Note: the schools display very well, but when I select a school, for instance the second school in the list, and submit the form it saves with only the first school's id in the list.

Thanks for your help.


Solution

  • The line tokenLimit: 1 means you can only select one 'school' at a time. Remove this to submit multiple.