ruby on rails - Using jQuery Autocomplete -


in rails 4 application, have search box in navbar using jquery's autocomplete. i'm using on user's index action. works good, except if click on user's name dropdown, want go profile page.

users controller:

def index   if params[:term]     @users = user.order(:name).where("lower(name) ?", "%#{params[:term]}%")     render json: @users.map(&:name)   end   if params[:users_name]     @users = user.search(params[:users_name])   end end 

search bar:

<%= form_tag users_path, :method => 'get', :role => 'search', :class => "navbar-form navbar-right" %>   <div class="form-group has-feedback">     <%= autocomplete_field_tag :users_name, '', users_path, :placeholder => "search", :required => true, :class => "form-control transition" %><span class="glyphicon glyphicon-search form-control-feedback"></span>   </div> <% end %> 

and routes users basic just

resources :users 

users/index.html.erb:

<div class="row">   <div class="span12">     <% if @users %>       <ol class="list-inline">         <%= render @users %>       </ol>     <% end %>   </div> </div> 

_user.rb:

i have appropriate file want displayed here well. 

like mentioned, autocomplete works fine, when start typing names, users name dropdown. problem i'm having if start typing justin , "justin timberlake" 1 of dropdowns, , click on justin timberlake want go profile page profile_page(@user)

i sure there few ways achieve this, 1 simple solution doing this...

users_controller.rb

def index   if params[:term]     @users = user.order(:name).where("lower(name) ?", "%#{params[:term]}%")     payload = []     # build data set     @users.each |u|       payload.push({label: u.name, value: u.id})     end     render json: @users.map(&:name)   elsif params[:users_name]     @users = user.search(params[:users_name])   end end 

in js code

$("#element_name").autocomplete({   source: "/user",   ...   select: function (e, d) {     // redirect user profile page..     window.location = "/users/" + d.item.value;   } 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -