Will Paginate: How change default page in links - ruby-on-rails

I have two partial. The first have a common will_paginate but in the second will_paginate I need to change the links (default url) generated by will_paginate.
Please I need their answer.
Thanks

You can use :param_name to change the query-string parameter:
<%= will_paginate #posts, :param_name => :posts_page %>
<%= will_paginate #comments, :param_name => :comments_page %>
Note that in your controller you must also change this:
#posts = Post.paginate :page => params[:posts_page]
#comments = Comment.paginate :page => params[:comments_page]

Try this
will_paginate(#some_collection, :params => { :controller => "foo", :action => "bar" })

Related

In Rails when using AJAX, how to change the js file it loads?

For example, I use data remote for the pagination like this :
<%= paginate #products, :remote => true %>
It works. But the problem is I have multiple lists to paginate in one page. So, I need a way to send the call to a separate files so I can render the appropriate content. How can I do this?
My index.js.erb :
<% if params[:list] %>
$('#products').html('<%= escape_javascript render(#products) %>');
$('#paginator').html('<%= escape_javascript(paginate(#products, remote: true, param_name: 'list' ).to_s) %>');
<% end %>
For this you need to add another param with paginate.
<%= paginate #products, :remote => true, :param_name => param_name %>
This params name will pass as parameter to controller so you can control which ajax pagination you are calling.
If you are calling same action then on the basis of param name you can change the value of #products also. One more thing you need to consider in your xxxx.js.erb file you need to render paginate object also.
In your js file you can change html on the basis of param also.
- if params[:xxx]
$('#xxx').html('#{escape_javascript render(:partial =>"partial_name",:locals => { :posts => #products, :param_name => 'xxx'})}');
$('#xxx #paginator').html('#{escape_javascript(paginate(#products, :remote => true, :param_name => 'xxx').to_s)}');
- if params[:yyy]
$('#yyy').html('#{escape_javascript render(:partial =>"partial_name",:locals => { :posts => #products, :param_name => 'yyy'})}');
$('#yyy #paginator').html('#{escape_javascript(paginate(#products, :remote => true, :param_name => 'yyy').to_s)}');
In your controller you should use param_name instead of params page

Is it possible to add an anchor/param to links with Kaminari?

I'm using pagination with Kaminari. It's working awesome.
One thing that I want for now is, adding #comment_section behind the url that Kaminari generates.
For example, my views is just like this. I'd like it to goes to the top of this section when page is loaded by clicking the link that Kaminari generated.
Is it possible?
<a name="comment_section">
<span id="comment">
<%= render 'users/comment' %>
</span>
<%= paginate #comments, :window => 4 %>
From the Kaminari documentation:
<%= paginate #users, params: { controller: 'foo', action: 'bar'} %>
So I guess you can modify it to have an anchor param, in your case:
<%= paginate #users, params: { anchor: 'comment_section' } %>
Hope this helps!
I don't remember right but it should work.
in view
<%= paginate #posts, :remote => true, :param_name => "post_page" %>
<%= paginate #comments, :remote => true, :param_name => "comment_page" %>
in controller
#posts = Post.search(query).page(params[:user_page])
#comments = Post.search(query).page(params[:post_page])

Rails will_paginage

i am using will_paginate for pagination,
When i reach the last page and then click on Next link its again taking me to first page,
Actually i want to disable the Next button when i reach the last page.
please advise me how to solve this..
this is the code i used for pagination
<%= will_paginate #users,
:prev_label => "Preevious",
:next_label => "Next",
:page_links =>true,
:renderer => PaginationListLinkRenderer
%>
Controller:
#users = User.paginate :per_page => 5,
:page => params[:page],
:order => 'name ASC',
:conditions => ""
View:
<%= will_paginate #users,
:previous_label => "Previous",
:next_label => "Next",
:inner_window => "2",
:outer_window => "0" %>
Above is all my code and it works for me in will_paginate. Once reached down the last page, the "Next" link change to bolded text, so as the last page number (see image below).
I am using
<%= will_paginate #users %>
and it is working fine. It disables the next link when i reach the last page.

Partial not rendered via RJS

In my new.html.erb page, i use the following line to render a partial and it works fine.
<%= render :partial => "submissions/player_form", :locals => { :submission => #submission } %>
Now i want to render exactly the same partial via RJS
<p>Player Type: <%= f.select(:PLAYER_TYPE, $playersList, {:prompt => 'Select the Player Type'} %></p>
<%= observe_field("submission_PLAYER_TYPE", :frequency => 1,
:url => { :controller => 'submissions',
:action => :display_player_form },
:with => "'player='+value") %>
display_player_form.rjs:
page.replace_html 'observed_assay_form', :partial => 'submissions/player_form', :locals => {:submission => #submission }
Nothing is displayed!!
Am i missing something??
Thanks for helping me out with this :)
I finally figured it out. So here are my findings:
In the partial, include the form_for tag, just like in the original form--
<% form_for #object do |f| %>
In the action used when observing the field, in my case, 'display_player_form', create a new instance of the object(see below)
#object = Object.new
In your rjs file, enter the following:
page['id of div'].replace_html :partial => 'your_partial_name'
There you go...
Hope this helps
I would rename display_player_form.rjs to display_player_form.js.erb and have its contents look like this:
$("#observed_essay_form").html('<%=
escape_javascript(
render :partial => 'submissions/player_form', :locals => {:submission => #submission }
)
-%>');
$("img[src$='spinner.gif']:visible").hide(); // optional - hide any visible spinner.gif images
I use jQuery, not Prototype, by the way.

in rails using will_paginate and partials

I have a collection of people that is paginated with will_paginate
#people = Person.paginate :page => params[:page],
:limit => 10,
:conditions => ['company_id = ? ' , #company.id ]
The people are shown on the company/view page and rendered with a partial. Note that the partial is in the views of 'people'
<%= render :partial => "people/person" , :locals => { :people => #people }%>
in the partial ...
<% for person in #people %>
...
<%end%>
<%= will_paginate #people %>
Now the partial does work, it renders all of the people and shows the paginate links on the bottom. However it doesn't not actually paginate the collection and instead it shows everything on the first page.
I am clearly missing something rather basic.
Thanks in advance.
Are you missing per_page?
Per_page should be the problem.
Also make :page => params[:page] look like :page => params[:page] || 1 so that will_paginate will stop complaining on blank page parameters.

Resources