Pagination not working only with a certain query - ruby-on-rails

I am using Kaminari gem to paginate using ajax
i have three paginations on the same page
#all_questions = Question.where.not(id: current_user.question_ids).page(params[:unanswered]).per(1)
#wrong = Tracker.where("user_id = ? AND correct = ?", current_user, false).page(params[:wrong_answers]).per(1)
#answered = Tracker.where("user_id = ? AND answered = ?", current_user, true).page(params[:all_answered]).per(1)
while the last two of the above instance variables correctly work. The first one when i click the next button, while i see the ajax request happening in the rails console, it does not refresh the page.
in my view
<%= paginate #all_questions, :remote => true, :param_name => "unanswered" %>
<%= paginate #wrong, :remote => true, :param_name => "wrong_answers" %>
<%= paginate #answered, :remote => true, :param_name => "all_answered" %>
Anyone knows why?
html
<section role="tabpanel" aria-hidden="true" class="content" id="panel2-2">
<div class="row">
<div class="large-10 large-offset-1 columns">
<div class="panel questions-progress-panel">
<ul>
<div id="unanswered">
<%= render partial: "all_questions" %>
</div>
<div id="paginator4" class="text-center paginator">
<%= paginate #all_questions, :remote => true, :param_name => "unanswered" %>
</div>
</ul>
</div>
</div>
</div>
</section>
#all_questions partial
<% #all_questions.each do |q| %>
<li>
<div class="wrapper">
<div class="row">
<div class="large-8 medium-8 small-8 columns">
<p class="question-title"> <%= q.description %> </p>
</div>
<div class="large-4 medium-4 small-4 columns text-right">
<%= link_to "Go", category_question_path(q.category,q), class:"button round success" %>
</div>
</div>
</div>
</li>
<% end %>

Your corresponding js.erb could look like this:
$('#unanswered').html('<%= escape_javascript render(all_questions) %>');
$("#paginator4").html('<%= escape_javascript(paginate(#all_questions, :remote => true).to_s) %>');

Related

rails leaving out some parts from fragment caching

I have a rails 4 app using pundit gem for authorization. If I do russian-doll fragment caching like the code below, the conditional statement used for authorization will be also cached, which is not good, since edit/delete buttons should only be available for the post.user.
What is the good way to get around this? Should I split the cache into smaller parts or is there a way to exclude some parts of the caching? What's the rails convention in this case?
index.html.erb
<% cache ["posts-index", #posts.map(&:id), #posts.map(&:updated_at).max, #posts.map {|post| post.user.profile.updated_at}.max] do %>
<%= render #posts %>
<% end %>
_post.html.erb
<% cache ['post', post, post.user.profile ] do %>
<div class="row>
<div class="col-md-2">
<%= link_to user_path(post.user) do %>
<%= image_tag post.user.avatar.url(:base_thumb), class: 'post-avatar' %>
<% end %>
</div>
<div class="col-md-8">
<span class="post-user-name"><%= post.user.full_name %></span>
<span class="post-updated"><%= local_time_ago(post.updated_at) %></span>
<div class="post-body">
<%= post.body %>
</div>
<div class="col-md-2" style="text-align:right;">
<!--############### THIS IS THE PART THAT SHOULD NOT BE CACHED #############-->
<% if policy(post).edit? && policy(post).delete? %>
<li class="dropdown">
<ul class = "dropdown-menu dropdown-menu-right">
<li>
<%= link_to "Edit Post", edit_post_path(post), remote: true, type: "button", 'data-toggle' => "modal", 'data-target' => "#updatepost_#{post.id}" %>
</li>
<li>
Delete Post
</li>
</ul>
</li>
<% end %>
<!--########################## UNTIL HERE ############################-->
</div>
</div>
<div class = "row comment-top-row" style="padding-bottom:10px;">
<div class="col-md-12 post-comment-form">
<%= render partial: 'posts/post_comments/post_comment_form', locals: { post: post } %>
</div>
</div>
<div class = "row">
<div class="col-md-12 post-comment-insert-<%= post.id%>">
<%= render partial: 'posts/post_comments/post_comment', collection: post.post_comments.ordered.included, as: :post_comment, locals: {post: post} %>
</div>
</div>
<% if policy(post).edit? %>
<div class="modal fade updatepost" id="updatepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<!-- FORM GETS RENDERED HERE VIA JS -->
</div>
<% end %>
<% if policy(post).delete? %>
<div class="modal fade" id="deletepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
......
</div>
<% end %>
<% end %>
Russian Doll Caching is simple but handy way for caching, there's no complex options or convention to exclude part of fragment from it. Above that, It's more relative to cache strategies. Here are two strategies for this user specific situation:
Rearrange individually and Cache fragments manually, which I don't recommend. Because it's more complex and doesn't leverage the advantages of Russian Doll Caching. Not so maintainable as well. Here is an example:
index.html.erb
<% # pull out cache %>
<%= render #posts %>
_post.html.erb
<% cache post %>
<%= # first part %>
<% end %>
<% # without cache %>
<%= # user specific part %>
<% cache post %>
<%= # third part %>
<% end %>
Preferred way: Add current_user as part of cache_key, which means you will have as many fragment caches as approximately your users and the fragments will automatically invalidate whenever the post or the user has changed their fingerprint. This is more elegant and maintainable. Here is an example:
index.html.erb
<% cache ["posts-index", #posts.map(&:id), #posts.map(&:updated_at).max, #posts.map {|post| post.user.profile.updated_at}.max] do %>
<%= render #posts %>
<% end %>
_post.html.erb
<% cache ['post', post, post.user.profile, current_user ] do %>
<div class="row>
<div class="col-md-2">
<%= link_to user_path(post.user) do %>
<%= image_tag post.user.avatar.url(:base_thumb), class: 'post-avatar' %>
<% end %>
</div>
<div class="col-md-8">
<span class="post-user-name"><%= post.user.full_name %></span>
<span class="post-updated"><%= local_time_ago(post.updated_at) %></span>
<div class="post-body">
<%= post.body %>
</div>
<div class="col-md-2" style="text-align:right;">
<% if policy(post).edit? && policy(post).delete? %>
<li class="dropdown">
<ul class = "dropdown-menu dropdown-menu-right">
<li>
<%= link_to "Edit Post", edit_post_path(post), remote: true, type: "button", 'data-toggle' => "modal", 'data-target' => "#updatepost_#{post.id}" %>
</li>
<li>
Delete Post
</li>
</ul>
</li>
<% end %>
</div>
</div>
<div class = "row comment-top-row" style="padding-bottom:10px;">
<div class="col-md-12 post-comment-form">
<%= render partial: 'posts/post_comments/post_comment_form', locals: { post: post } %>
</div>
</div>
<div class = "row">
<div class="col-md-12 post-comment-insert-<%= post.id%>">
<%= render partial: 'posts/post_comments/post_comment', collection: post.post_comments.ordered.included, as: :post_comment, locals: {post: post} %>
</div>
</div>
<% if policy(post).edit? %>
<div class="modal fade updatepost" id="updatepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<!-- FORM GETS RENDERED HERE VIA JS -->
</div>
<% end %>
<% if policy(post).delete? %>
<div class="modal fade" id="deletepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
......
</div>
<% end %>
<% end %>

How to filter results by clicking on checkbox in ruby on rails

I am a newbie in ruby on rails it's also my first ruby application.
I want to filter results by clicking on checkbox which is coming from
the database and showing on the right side of my webpage, the checkbox
is lying on the left side. In the below I attached my webpage's
screenshot for easy understanding.
Anyone can help me, please, how can I solve this issue?
My Controller file code:resumes_controller.rb
class ResumesController < ApplicationController
before_filter :recruiter!
def resumes
#resume = Jobseeker.paginate(page: params[:page], :per_page => 10).order('jobseeker_id DESC')
end
end
My view file code: resumes.html.erb
<%= form_for :jobseekers,url: resumes_path(#jobseekers), action: :update, method: :post do |f| %>
<div>
<label for="Title">Education</label>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Masters
</div>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Bachelor
</div>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Associates
</div>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Diploma
</div>
More
</div>
<% end %>
<!-- item1 -->
<% #resume.each do | res| %>
<div class="job-item bio-card">
<div class="employer-information">
<div class="col-sm-6">
<ul class="list-unstyled">
<li class="employ-name">
<a href="<%= view_profile_path(:jobseeker_id => res.jobseeker_id) %>">
<%= res.first_name %> <%= res.last_name %></a> <span>- <%= res.current_address %></span>
</li>
<li><%= res.institute_name %></li>
</ul>
</div>
<% if res.attach_resume.nil? %>
<div class="col-sm-6 employ-pdf-box">
<div class="employ-pdf">
<i class="fa fa-file-powerpoint-o icon"></i>
<h3>Empty</h3>
</div>
</div>
<% else %>
<div class="col-sm-6 employ-pdf-box">
<a href="<%= res.attach_resume.resume %>"target="_blank">
<div class="employ-pdf">
<i class="fa fa-file-powerpoint-o icon"></i>
<h3><%= res.first_name %> <%= res.last_name %>.pdf</h3>
</div>
</a>
</div>
<% end %>
</div>
</div>
<% end %>
<!-- End item1 -->

Ruby on Rails - dynamic navigation with link_to in a div

I want to add navigation to a series of div, which are loaded dynamically.
For this I am using link to covering the div and passing it the attribute to the method.
The problem is that when I press on the div the attribute does not correspond.
I put the code.
With this code all it's ok, but i don't have the link in the div.
<%for i in inicio..final %>
<div class="col-md-3">
<div class="tarjeta_comercio">
<%= link_to 'mezcla/mostrar_institucion', {:controller => "mezcla", :action => "mostrar_institucion", :institucion_actual => #institucions[azar[i]].id} %>
<div class="foto">
<%= image_tag #institucions[azar[i]].logo.url(:original) %>
</div>
<div class="titulo">
<%= #institucions[azar[i]].nombre %>
</div>
<% end %>
But the problem is when i do a block with "link_to...do-div-end". If i do this the parameter don't run. I don't explain why this happen. This is the wrong code.
<%for i in inicio..final %>
<div class="col-md-3">
<div class="tarjeta_comercio">
<%= link_to 'mezcla/mostrar_institucion', {:controller => "mezcla", :action => "mostrar_institucion", :institucion_actual => #institucions[azar[i]].id} do %>
<div class="foto">
<%= image_tag #institucions[azar[i]].logo.url(:original) %>
</div>
<div class="titulo">
<%= #institucions[azar[i]].nombre %>
</div>
<% end %>
<% end %>
In :institucion_actual each time put 0 (when i pulsed over).
Thanks.

how to refresh a partial after an edit has been made in rails

I am making an ecommerce website page.It consists of two parts.A sidebar having a list of all products and a right column where the details of the product is shown once the product is selected from the list.
I am making ajax calls to load the various partials of the application. The sidebar having all the products is index,the one on right having the details of products is show which shows the details of the product selected.
Now when I edit the product(through a modal) how do I automatically reload the respective partials so that the new changes are reflecte.For me to see the changes I have to refresh the pages currently,how do i ensure the partials are automatically loaded once I click submit on the edit modal.
My index.html.erb looks like
<div class="container">
<div class="row row-offcanvas row-offcanvas-left">
<div class="col-xs-8 col-sm-4 sidebar-offcanvas" id="sidebar" role="navigation">
<div class="well sidebar-nav">
<ul class="nav">
<div class="jumbotron well">
<h3>Product Listing Application</h3>
</div>
<%= link_to "New Product", new_product_path, remote: true, class: "btn btn-primary" %>
<div class="list-group">
<%= render "index" %>
</div>
</ul>
</div><!--/.well -->
</div><!--/span-->
<div class="col-xs-10 col-sm-8">
<div id="page-content-wrapper">
<div class="page-content">
<div class="col-md-12 product-info" id="product-details">
</div>
</div>
</div>
<div id="product-modal" class="modal fade"></div>
</div>
</div><!--/row-->
</div>
My _index.html.erb is
<% #products.each do |product| %>
<%= link_to product_path(product), remote: true, class: "list-group-item" do %>
<h4 class="list-group-item-heading"><%= product.name %></h4>
<p class="list-group-item-text"><%= number_to_currency product.price %></p>
<% end %>
<% end %>
My _edit.html.erb is
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3><%= "Editing #{#product.name}" %></h3>
</div>
<%= render "form" %>
</div>
</div>
My _form.html.erb is
<%= form_for #product, remote: true, html: { class: "form-horizontal", style: "display:inline;" } do |f| %>
<div class="modal-body">
<ul class="errors"></ul>
<div class="control-group">
<%= f.label :name, class:"control-label" %>
<div class="controls">
<%= f.text_field :name %>
</div>
</div>
<div class="control-group">
<%= f.label :price, class: "control-label" %>
<div class="controls">
<%= f.text_field :price %>
</div>
</div>
</div>
<div class="modal-footer">
<%= f.submit class: "btn btn-primary" %>
<%= link_to "Cancel", "#", class: "btn", data: {dismiss: "modal"} %>
</div>
<% end %>
You can use
location.reload();
on your js.erb for refresh your partial, or for your instance vaiable
you can directly use
#post = #post.reload # in palce of #post you can your instance variable
create a js file same name as your page
add below code in it
edit.js.erb
$("<%= escape_javascript render(:partial => 'ur_partial_page_name') %>").dialog();
$('#new_comment_link').hide();`
in your edit page form where u r calling modal dialog
<%= link_to "edit", edit_product_path (#product, :format => :js), :remote => true %>
In your product controller
def edit
#source = Product.find(params[:id])
respond_to do |format|
format.html # edit.html.erb
format.xml { render :xml => #product }
format.js
end
end
and in your partial form
<%= form_for (#product, :remote => true) do |f| %>

Can't find root of 'stack level too deep' error when rendering partial

I've been stuck on this problem for a week. I will mail you a bottle of scotch if you can figure it out. Seriously, its come to bribery.
On taxons#show I'm rendering a products partial, _products.html.erb, which lists out all the products on a table to the show view of the taxons controller. When you click a product, by default the app will redirect the user to products#show, where the _cart_local.html.erb partial is rendered to display 'add to cart' options.
But on taxons#show, when a product is clicked I bring up a lightbox instead so the user doesn't have to leave the page. The lightbox code is inside _products.html.erb, and I'm trying to render _cart_form.html.erb inside of the lightbox. When I do, I get the 'stack level too deep' error and taxons#show won't render.
But the cart renders fine in products#how. I changed #product in the partial to just product. That didn't help. I rendered an empty partial and the page loads, which makes me think the problem is with _cart_local (but why would it render on products#show?).
Then I took out all of the code in between the opening form tag and the ending div/end tags and the page also rendered, which makes me think its in that block, but I can't wittle it down any further. I'm stuck
Here's the code for _cart_local, and if I take out the code between the <!-- Here --> and <!-- AND HERE --> comments, the page renders:
<%= form_for :order, :url => populate_orders_path do |f| %>
<div id="inside-product-cart-form" data-hook="inside_product_cart_form" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<% if product.has_variants? %> <!-- HERE -->
<div id="product-variants" class="columns five alpha">
<h6 class="product-section-title"><%= t(:variants) %></h6>
<ul>
<% has_checked = false
product.variants.active(current_currency).each_with_index do |v,index|
next if v.option_values.empty? || (!v.in_stock && !Spree::Config[:show_zero_stock_products])
checked = !has_checked && (v.in_stock || Spree::Config[:allow_backorders])
has_checked = true if checked %>
<li>
<%= radio_button_tag "products[#{product.id}]", v.id, checked, :disabled => !v.in_stock && !Spree::Config[:allow_backorders], 'data-price' => v.price_in(current_currency).display_price %>
<label for="<%= ['products', product.id, v.id].join('_') %>">
<span class="variant-description">
<%= variant_options v %>
</span>
<% if variant_price v %>
<span class="price diff"><%= variant_price v %></span>
<% end %>
</label>
</li>
<% end%>
</ul>
</div>
<% end%>
<% if product.price_in(current_currency) and !product.price.nil? %>
<div data-hook="product_price" class="columns five <% if !product.has_variants? %> alpha <% else %> omega <% end %>">
<div id="product-price">
<h6 class="product-section-title"><%= t(:price) %></h6>
<div><span class="price selling" itemprop="price"><%= product.price_in(current_currency).display_price %></span></div>
</div>
<div class="add-to-cart">
<% if product.on_sale? %>
<%= number_field_tag (product.has_variants? ? :quantity : "variants[#{product.master.id}]"),
1, :class => 'title', :in => 1..product.on_hand, :min => 1 %>
<%= button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do %>
<%= t(:add_to_cart) %>
<% end %>
<% else %>
<%= content_tag('strong', t(:out_of_stock)) %>
<% end %>
</div>
</div>
<% else %>
<div id="product-price">
<br>
<div><span class="price selling" itemprop="price"><%= t('product_not_available_in_this_currency') %></span></div>
</div>
<% end %> <!-- AND HERE -->
</div>
<% end %>
And here is _products.html.erb, the file that is loading all the products, contains the lightbox, and has the render cart partial code:
<div class="overlay-container">
</div>
<%
paginated_products = #searcher.retrieve_products if params.key?(:keywords)
paginated_products ||= products
%>
<% if products.empty? %>
<%= t(:no_products_found) %>
<% elsif params.key?(:keywords) %>
<h6 class="search-results-title"><%= t(:search_results, :keywords => h(params[:keywords])) %></h6>
<% end %>
<div class="product_grid_container">
<div class="grid_2"><%= image_tag("store/featured/#{#featured}.jpg") %></div>
<% if products.any? %>
<ul id="products" class="inline product-listing" data-hook>
<% products.each do |product| %>
<% if product.on_display? %>
<%# ******LIGHTBOX******* %>
<div id="product_popup_<%= product.id %>" class="product_popup" data-popid="<%= product.id %>">
<div class="related-products">
<ul class="related_products_list" id="related_products_list_<%= product.id %>" data-listid="<%= product.id %>">
<% #related_products.each do |related_product| %>
<li class="related_products_item"><%= link_to large_image(related_product, :itemprop => "image", :data => {:imageid => related_product.id}, :id => "related_" + related_product.id.to_s, :class => "related_products_image dimmed"), url_for(related_product) %></li>
<% end %>
</ul>
</div>
<div class="popup-image">
<%= large_image(product, :itemprop => "image", :class => "product-image-popup") %>
</div><!-- popup-image -->
<div class="popup_right_content">
<h2 class="popup-title"><%= product.name %></h2>
<p class="popup-price">$<%= product.price %></p>
<p><%= product.description %></p>
<p class="popup-color">color:</p>
<div class="popup-images" data-productid="<%= product.id %>">
<% if (product.images + product.variant_images).uniq.size > 1 %>
<ul id="popup-thumbnails-taxon" class="thumbnails inline" data-hook>
<% product.images.each do |i| %>
<li class='tmb-all' id='tmb-<%= i.id %>'>
<%= link_to(image_tag(i.attachment.url(:small)), i.attachment.url(:popup), :class => 'tmb-all', :id => "tmb-#{i.id}") %>
</li>
<% end %>
</ul>
<% end %>
</div><!-- popup-images -->
</div><!-- popup_right_content -->
<%= render 'spree/shared/cart_local', :locals => {:product => product} %>
</div><!-- product_popup -->
<%# ******END LIGHTBOX******* %>
<div class="grid_1">
<li id="product_<%= product.id %>" class="columns product three <%= cycle("alpha", "secondary", "", "omega secondary", :name => "classes") %>" data-hook="products_list_item" itemscope itemtype="http://schema.org/Product">
<div class="main-image" id="single_<%= product.id %>" data-productid="<%= product.id %>">
<%= link_to large_image(product, :itemprop => "image", :class => "product-image", :id => product.id), product_path(product), :remote => true, :html => {:class => "product_popup"} %>
</div><!-- main-image-->
<div class="prod_info_box">
<%= link_to truncate(product.name, :length => 50), product, :class => 'info', :itemprop => "name", :title => product.name %>
<span class="price selling" itemprop="price"><%= product.price_in(current_currency).display_price %></span>
<!-- BRINGS THUMBNAILS INTO TAXONS PAGE -- PULLED FROM _THUMBNAILS.HTML.ERB -->
<div class="product-images" data-productid="<%= product.id %>">
<% if (product.images + product.variant_images).uniq.size > 1 %>
<ul id="product-thumbnails-taxon" class="thumbnails inline" data-hook>
<% product.images.each do |i| %>
<li class='tmb-all' id='tmb-<%= i.id %>'>
<%= link_to(image_tag(i.attachment.url(:mini)), i.attachment.url(:normal), :class => 'tmb-all', :id => "tmb-#{i.id}") %>
</li>
<% end %>
</ul>
<% end %>
</div><!-- product-images -->
<!-- END THUMBNAILS INTO TAXONS PAGE -->
<div id="product-description-taxon">
<p><%= product.description %></p>
</div><!-- product-description-taxon -->
</div><!-- prod_info_box -->
</li>
</div>
<% end %>
<% end %>
<% reset_cycle("classes") %>
</ul>
<% end %>
</div><!-- product_grid_container -->
<% if paginated_products.respond_to?(:num_pages) %>
<%= paginate paginated_products %>
<% end %>
Let me know if you need anything else. i appreciate it.
Here's a link to the helpers, maybe the problem is there?
https://github.com/spree/spree/tree/v1.3.2/core/app/helpers/spree
In your _products.html.erb partial change that:
<%= render 'spree/shared/cart_local', :locals => {:product => product} %>
to that:
<%= render partial: 'spree/shared/cart_local', :locals => {:product => product} %>
and problem should be resolved.
Why? Because doing it as you have done it won't pass locals into partial and that's why you've received error, check it by yourself by removing locals. Of course the most interesting part is why you get stack level too deep error here, but I am unable to find answer for that now.
Oh, and debugger is your friend ;)

Resources