how to paginate combined with anchor id - ruby-on-rails

I'm very new to Ruby on Rails and learning while trying to fix some bugs on my company website. I'm trying to paginate a collection of records combined with a particular anchor, i.e. when user clicks on the next/previous page, the pagination happens and user lands on a particular section of the page. This is how my code looks at the moment:
view
<%= page_navigation_links #student_logs, :page %></p>
controller:
#student_logs.paginate(:page => params[:page], :per_page => 10)
application_helper
def page_navigation_links(pages, param_name=:page)
will_paginate(pages, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '←'.html_safe, :next_label => '→'.html_safe, :param_name => param_name)
end
This works fine for my pagination, but I need to fit a reference to an 'anchor id' into this code, like Student Logs, so that when user navigates to a different page the browser navigates the user to the heading with id: "student_logs".
I can't figure out how to do this. Can anyone help?

I do not have direct experience with it. However, this link shows a way. I could not test but you can certainly do.
<%= will_paginate #posts, :params => {:anchor => i} %>
If this works, then you can utilize this way in your helper:
def page_navigation_links(pages, param_name=:page)
will_paginate(pages, :params => {:anchor => "#ANCHOR"}, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '←'.html_safe, :next_label => '→'.html_safe, :param_name => param_name)
end
You can replace #ANCHOR as you need.

Related

How can I use 'in-page link' by # when using Kaminari?

I have this in my view
<%= paginate #comments, :window => 4, :outer_window => 2 %>
This automatically creates pagination links. If I click one of them, it takes me to
http://example.com/shop/walmart?page=2
How can I add in-page link, which takes me to this url?
http://example.com/shop/walmart?page=2#abc
Supposing #abc is the destination here.
<%= paginate #comments, :window => 4, :outer_window => 2 , :params => {:anchor => "abc"} %>

PAPERCLIP: How to list only users who have a image attached?

I have the "paperclip" working in my app.
So, I want to list only chefs who have a image attached.
ruby 1.9.2p290
rails 3.1.1
In HomeController
#chefs = Chef.where({:status_id => [0,1]}).all(:order => 'created_at DESC', :limit => 10)
In Home Index view
<% #chefs.each do |chef| %>
<%= image_tag chef.avatar.url(:thumb), :height => '50', :width => '50' %>
<% end %>
Is there way to put this condition in #chefs var?
If don't... how can I do this? I'm new in ruby/rails.
Thanks!
You probably want this in HomeController:
#chefs = Chef.where({:status_id => [0,1]}).where('avatar_file_name is not null').all(:order => 'created_at DESC', :limit => 10)
That will select only chefs whose status id is 0 or 1 and whose avatar file name is not null.

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.

Ajax observe_field not working on ruby on rails

I am trying to create some kind of search in my ruby on rails application and I want to add ajax support. I want when a user types in a search box a word, ajax automatically generates the results. And I have searched almost all internet and I couldn't find answer to following question:
When I type into my text area nothing happens. It is like ajax is not working at all.
Tre problem is in observe_field which should observe my id= 'query' but it seem like it not doing anything
I am using Prototype 1.6.1 and the newest version of rails.
The code I wrote is:
viewer:
<form name="sform" action="" style="display:inline;">
<label for="item_name">Filter on Property No : </label>
<%= text_field_tag(:query, params['query'], :size => 10, :id => 'query' ) %>
</form>
<%= image_tag("/images/system/blue_small.gif",
:align => "absmiddle",
:border => 0,
:id => "spinner",
:style =>"display: none;" ) %>
</p>
<%= observe_field 'query', :frequency => 1,
:update => 'table',
:before => "$('#spinner').show()",
:success => "$('#spinner').hide()",
:url => {:action => 'list'},
:with => 'query' %>
<div id="table">
<%= render :partial => "items_list" %>
</div>
And my controler is:
def list
sort = case params['sort']
when "Title" then "title"
when "address" then "address"
when "close date" then "close_date"
when "property_no_reverse" then "property_no DESC"
when "address_reverse" then "address DESC"
when "close_date_reverse" then "close_date DESC"
end
conditions = ["title LIKE ?", "%#{params[:query]}%"] unless params[:query].nil?
#total = Message.count(:conditions => conditions)
#accounts = Message.paginate :page => params[:page], :per_page => 10, :order => sort, :conditions => conditions
if request.xml_http_request?
render :partial => "items_list", :layout => false
end
end
I would greatly appreciate any help I can get!
You said your using the newest version of Rails, if that is Rails 3.0.0 then observe_field and the way they are doing javascript is definitely different. Either downgrade to Rails 2.3.8 (I would not), learn the new way, or use the old way in Rails 3.0.0
New Way in Rails 3.0.0: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/
Old way in Rails 3.0.0: http://github.com/rails/prototype_legacy_helper
If you want 2.3.8 I can show you observe field, just let me know...

Question regarding RJS and usage of link_to_remote

I was working on my website when I cam across this problem, I have 100 posts in my blog and I am paginating them 10 at a time, I display the 1st 15 and display a link at the bottom which basically is implemented using the link_to_remote tag.
<%= link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'} %>
I click on that and get the next 15 posts, and append it to the container containing the 1st 15 posts through insert_html
page.insert_html :bottom, :puzzles , :partial => 'puzzles', :object => #puzzles
Now what I want is the link at the bottom of the page to update too, so that the next time the user clicks more the 3rd batch is fetched and so on. Basically something like
page.replace_html 'more-link', link_to_remote "More Posts", :url => {:action => 'view' ,:id => link.to_i + 1} , :html => {:id => 'more-link'}
Any clues as to how I can do it?
You're very close.
replace_html is called with (DOM_id, options for render). Essentially you want to render the output of a link_to_remote call. But you're not passing it in a form that render can use. As Barry Hess points out replace is much better suited for this task, because most of what you're changing is the tag attributes.
Using replace_html would result in nested tags which is could cause problems. You want to replace the element entirely. Replace has the same syntax of replace_html so you would run into the same problems if you were just switch replace_html to replace.
Here's what you want:
page.replace 'more-link', :text => link_to_remote "More Posts",
:url => {:action => 'view' ,:id => link.to_i + 1} ,
:html => {:id => 'more-link'}
However I'm not sure if link_to_remote is accessible from RJS. If the above doesn't work you could always do this:
page.replace 'more-link', :inline => "<%= link_to_remote 'More Posts',
:url => {:action => 'view' ,:id => link.to_i + 1},
:html => {:id => 'more-link'} %>", :locals => {:link => link}

Resources