ajax will_paginate

60 views
Skip to first unread message

Melb01

unread,
Feb 9, 2015, 6:13:46 AM2/9/15
to rubyonra...@googlegroups.com
Hi,
I have an index page with searchbox and with pagination, it worked well
when I tried to make pagination with javascript, itworked for the first page but not the second
I think the problem is to pass the value of the search box to the script of pagination

here is my code:
------------------------------------------------------------------------------------
index.hmtl.erb

<%= text_field_tag 'q', params['q']  %>
<div id="posts" >  
        <%= render  'layouts/posts' %>
</div>
<% if !@posts.nil? %>
      <%= link_to 'Load More Posts', post_path(:page => @posts.next_page), :class => 'load-more-posts', :remote => true if @posts.next_page %>
 <% end %>
-----------------------------------------------------------------
_posts.html.erb
<% if !@posts.nil? && @posts.size > 0  %>
                <% @posts.each_with_index do |post| %>
                    <b><%= post.title %></b>
                    <p><%= post.body %></p>
                <% end %>
 <% end %>
-----------------------------------------------------


index.js.erb
$("#posts").append("<%= escape_javascript(render partial: "layouts/posts", :locals => { :posts => @posts }) %>");
---------------------------------------------------------------------------
post_controller.rb

if !params[:q].nil?  
     
      @posts = Post.find_with_q.paginate(:per_page => 10, :page =>  params[:page])
     
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @posts }
        format.js
      end
end

--------------------------------------------------------------------------------


The problem with this code is thatI can't go to the next page beause the params q is null with ajax call
when I delete the condition it works well
thank you fo the help

rgds,



tamouse pontiki

unread,
Feb 9, 2015, 11:25:03 AM2/9/15
to rubyonra...@googlegroups.com
Maybe what you want is something more like:

if params[:q]
  @posts = Posts.find_with_q.paginate...
else
  @posts = Posts.all.paginate...
end

respond_to do |format|
  ...
end




So your rendering isn't gated by whether there is a 'q' parameter; it only determines what gets set into @posts.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b53cd303-5228-4885-814b-44eaade8c4c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tamouse pontiki

unread,
Feb 9, 2015, 11:25:13 AM2/9/15
to rubyonra...@googlegroups.com
... except with the right model name ...

Melb01

unread,
Feb 9, 2015, 12:14:29 PM2/9/15
to rubyonra...@googlegroups.com
Thx for the answer
but my problem is when I have a query, I am unable to go to the second page
I get @posts as nil object, it is like paginating with ajax do not submit the query when changing the page

rgds,

tamouse pontiki

unread,
Feb 9, 2015, 5:13:35 PM2/9/15
to rubyonra...@googlegroups.com
State your problem more clearly, as I don't understand. Better, post the code you have and what you're actually expecting.

Melb01

unread,
Feb 10, 2015, 5:05:51 AM2/10/15
to rubyonra...@googlegroups.com
I already posed my code, I have a paginated list with will_paginate obtained with a search query
the first page is shown correctly but the second is not shown with the js code because the query is not submitted for the second page with ajax code
the params[:q] is nil when I navigate to the second page
def index

    if !params[:q].nil?  
     
      @posts = Post.find_with_q.paginate(:per_page => 10, :page =>  params[:page])
     
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @posts }
        format.js
      end
   end

end



gribanov...@gmail.com

unread,
Feb 11, 2015, 2:58:17 AM2/11/15
to rubyonra...@googlegroups.com
Hi! I think this will help you http://railscasts.com/episodes/240-search-sort-paginate-with-ajax

вторник, 10 февраля 2015 г., 13:05:51 UTC+3 пользователь Melb01 написал:

Melb01

unread,
Feb 11, 2015, 8:57:53 AM2/11/15
to rubyonra...@googlegroups.com
Yes, I found that too while googling,I think it is helpful
Thx
Reply all
Reply to author
Forward
0 new messages