Wednesday 15 March 2017

Auto suggest in rails for fields it so simple by help of data-autocomplete parameter

 
Define data-autocomplete field in text field and then path of the function with parameters: 
 
=f.text_field :user_search,"data-autocomplete" => autocomplete_user_name_path(:user_type=>"Admin")
 
 
Controller Action:
 
def autocomplete_institution_name
  term = params[:term]
  user_type = params[:user_type]
  
  users = User.where('name LIKE ? AND user_type = ?', "%#{term}%",user_type).order(:name).all 
  render :json => users.map { |user| {:id => user.id, :label => user.name, :value => user.name} }
end 
 

No comments:

Post a Comment