2

I have a Listing#index page, which display 50 listings. I would like to have a button "More" that trigger javascript to query my MySql database for additional listings, then append to existing 50 listings without refreshing the page.

How should I go about this? I am using Rails and jQuery. I know Rails but new to Javascript.

Thank you very much for your help.

2
  • Pagination is your friend. Google "pagination rails". There are many options. Commented Feb 19, 2012 at 5:43
  • Thanks Marc. I can't use pagination in my case. Commented Feb 19, 2012 at 16:30

1 Answer 1

2

There are many ways to solve this problem.

1 You may render a "...js.erb" template which will perform some jquery code with append method (http://api.jquery.com/append/) or other manipulation logic. Do not forget to set :remote attribute to your button. I think this method will be easiest for you.

For example:

in controller:

def get_posts
  @posts.all # or something you need
  render "_get_posts.js.erb"
end

_get_posts.js.erb:

<% @posts.each do |post| %>
  $('#posts').append("<%= post.title %>");
<% end %>

Also in js.erb you may render partial or do something you want.

2 Bind logic to click event of your button (http://api.jquery.com/click/), than send ajax query to server with a variety of ways (http://api.jquery.com/category/ajax/), than render data you have chosen (json, html, xml) and on client-side in callback function of ajax catch it and append to your page.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your detailed advice. I am so new to Javascript that I still struggle with second step. Could you please give a very quick example how I should do it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.