Skip to content

Instantly share code, notes, and snippets.

@thiagofm
Created April 17, 2010 23:01
Show Gist options
  • Save thiagofm/369882 to your computer and use it in GitHub Desktop.
Save thiagofm/369882 to your computer and use it in GitHub Desktop.
I paginate the data
@books = Book.paginate :page => params[:page], :conditions => ['title LIKE ?', "%#{params[:q]}%"]
then respond requests
respond_to do |format|
format.html # new.html.erb
#format.xml { render :xml => @user_book}
format.js
end
As you see, i don't have any .js responding, but i do have a file named new.js.erb with:
<%= @books.collect(&:title).join("\n") %>
So what it basically does is paginate the results of "title" in each line.
Now i want to show that list into a textfield like google does, that list is composed of books so you don't have to actually type the book that ur reading if somebody else did
For that, i'm using in my view file:
<%= f.text_field :title, :onfocus => "$(##{:title}").autocomplete('http://0.0.0.0:3000/books/new/books.js')", :new => nil %>
I know i could work on a route and take out that address... but i'm new to rails and i want to do that project to learn and then find stuff that i could do better.
Thanks for the help!
@ahawkins
Copy link

module CustomersHelper
def customer_names_for_autocomplete
current_account.customers.map(&:to_s).to_json
end
end

<% content_for :head do %>

<script type="text/javascript"> $(function() { var companies = <%= company_names_for_autocomplete %>; $("#customer_company_name").autocomplete({source: companies}); }); </script>

<% end %>

<%= customer.input :company_name, :label => "Company" %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment