rails 4 simple fragment caching doesn't work - ruby-on-rails

I changed the following in config/enviornments/development to:
config.action_controller.perform_caching = true
I have the following view:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<% cache do %>
<% #products.each do |product| %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
</div>
</div>
<% end %>
<% end %>
I load the page, then remove the title and then load page again and the title disappears from the page. With caching I expect it to show the title. Is there any step I am missing?
EDIT:
IN rails guide example:
<% Order.find_recent.each do |o| %>
<%= o.buyer.name %> bought <%= o.product.name %>
<% end %>
<% cache do %>
All available products:
<% Product.all.each do |p| %>
<%= link_to p.name, product_url(p) %>
<% end %>
<% end %>
They seem to do the same/similar type of cache. I have tried some of their examples and nothing caches.

Related

Rails: Cloudinary gem - default image not loading

I'm working on a pinterest-like app, using Cloudinary gem with Attachinary. I have added a line into my index.html.erb file which was supposed to display a default image if a created Pin didn't have one or the image didn't load properly.
<% if pin.image.present? %>
<%= link_to pin do %>
<%= cl_image_tag(pin.image.path) %>
<% end %>
<% if pin.image.present? == false %>
<%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>
Unfortunately it doesn't work - the images for the pins having them display properly, but for ones who don't only a thick line is visible (jQuery Masonry grid). I'm wondering what is the proper way of setting a default image with cloudinary. Where did I go wrong? Here is my full index file:
<div id="pins" class="transitions-enabled">
<% #pins.each do |pin| %>
<div class="container">
<div class="box panel panel-default">
<% if pin.image.present? %>
<%= link_to pin do %>
<%= cl_image_tag(pin.image.path) %>
<% end %>
<% if pin.image.present? == false %>
<%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>
<div class="panel-body">
<%= pin.description %> <br>
<strong><%= pin.user.email if pin.user %></strong>
<% if pin.user == current_user && user_signed_in? %>
<div class="actions">
<%= link_to edit_pin_path(pin) do %>
<span class="glyphicon glyphicon-edit black"></span>
Edit
<% end %>
<%= link_to pin, method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-trash black"></span>
Delete
</div>
<% end %>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
I will only add that everything worked fine until I've introduced the glyphicons.
I will be greatful for your help!
If there was no copy&paste error, there is an extra end missing to terminate the link_to pin do statement. So the first if statement is still active, and then an opposite if statement is queried, which of course will never be true.
I suggest to use if-else-end.
<% if pin.image.present? %>
<%= link_to pin do %>
<%= cl_image_tag(pin.image.path) %>
<% end %>
<% else %>
<%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>

Dynamically generate all products on a responsive view using Bootstrap's Grid system

I want to generate all my products and their information on a page using bootstrap's grid system. I tried to generate three products in a row at first and it worked using the following code:
<div class="container">
<h1 align="center">Listing products</h1>
<% #products.each do |product| %>
<% if #a%3 == 0 %>
<div class="row">
<% end %>
<div class="col-lg-4">
<%= image_tag(product.image_url, class: 'list_image', size: '260x320') %>
<%= product.title %> <br/>
<%= product.price %> <br/>
<%= link_to 'Show', product %><br/>
</div>
<% #a = #a+1 %>
<% if #a%3 == 0 %>
</div><hr/>
<% end %>
<% end %>
</div>
(#a is what I declared in the controller which is initially set to 0)
The code will not work anymore if I want to only display two or less products in a row using the grid system when the screen gets smaller.
Are there any better ideas to achieve this?
Seems like you are generating incorrect HTML markup. Try to use each_slice:
<div class="container">
<h1 align="center">Listing products</h1>
<% #products.each_slice(3) do |products| %>
<div class="row">
<% products.each do |product| %>
<div class="col-lg-4">
<%= image_tag(product.image_url, class: 'list_image', size: '260x320') %>
<%= product.title %> <br/>
<%= product.price %> <br/>
<%= link_to 'Show', product %><br/>
</div>
<% end %>
</div>
<hr/>
<% end %>
</div>

erb block not looping

I'm trying to generate results for a webstore using this erb block and I want 4 images per row, but right now it is only generating one image per row. Any advice would be highly appreciated
<% n = 4 %>
<% #products.each do |product| %>
<div class="row-fluid">
<% if (n%4 == 0) %>
<% end %>
<div class="span3">
<%= link_to image_tag(product.images.order(:placement).first.image.url(:medium)), product if product.images.present? %>
<p class="text-center"><%= link_to product.name, product %></p>
</div>
<% if (n%4 == 3) %>
<% end %>
<% n += 1 %>
</div>
<% end %>
Ruby will break it into groups of four for you using Enumerable#each_slice:
<% #products.each_slice(4) do |row| %>
<div class="row-fluid">
<% row.each do |product| %>
<div class="span3">
<%= link_to image_tag(product.images.order(:placement).first.image.url(:medium)), product if product.images.present? %>
<p class="text-center"><%= link_to product.name, product %></p>
</div>
<% end %>
</div>
<% end %>

If Condition in each do Rails

Hi i need to print out just the candidates where active is == 0 here is my code in the view.
I can print if active is yes or no.. But in the each do loop i just want to print the active candidates.
So how can i add the condition to my each do loop, thanks.
<% #candidates.each do |candidate| %>
<div id="candidateper">
<div class="avatth" ><div class="avat_min">
<% if candidate.avatar.present? %>
<%= link_to (image_tag candidate.avatar.url(:thumb)), (candidate_path(candidate)) %>
<% else %>
<%= link_to (image_tag ("espanol/playersample.png")), (candidate_path(candidate)) %>
<% end %>
</div></div>
<div class="nameth"><%= candidate.name %></div>
<div class="activeth"><%= candidate.active ? t('generales.yess') : t('generales.noo') %></div>
<div class="generalth">
<% if candidate.user.purchased_at.present? %>
<%= candidate.user.purchase_defeated? ? t('generales.defeated') : t('generales.active') %>
<% else %>
<%= t('generales.noo') %>
<% end %>
</div>
<div class="actionsth"><%= link_to t('generales.show'), candidate_path(candidate) %>
<% if current_user.user_type == 'admin' %>
<%= link_to t('generales.delete'), candidate_path(candidate), method: :delete, data: { confirm: t('generales.delete_candidate_confirm') } %>
<% end %>
</div>
</div>
<% end %>
</div>
<% end %>
I`ve tried this
no luck syntax error on all my ideas :P
If candidate.active is actually a boolean then you could say:
<% #candidates.reject(&:active).each do |candidate| %>
...
<% end %>
If #candidates is actually an ActiveRecord::Relation then you could probably say:
<% #candidates.where(:active => false).each do |candidate| %>
...
<% end %>
to avoid pulling a bunch of stuff out of the database when you don't want it.
If active is actually a number (inside the database and outside the database) then you could say:
<% #candidates.select(&:zero?).each do |candidate| %>
...
<% end %>
or
<% #candidates.where(:active => 0).each do |candidate| %>
...
<% end %>

Rails Odd Even If Statement

I have a loop for my Activity model
<% #activities.each do |activity| %>
<div>
<div class="cell">
<%= image_tag(activity.image) %>
</div>
<div class="cell">
<h4><%= activity.title %></h4>
<p><%= activity.description %></p>
<span><%= activity.distance %></span>
</div>
</div>
<% end %>
I want to create a zig-zag effect, so I need to re-arrange the HTML for every even activity, so the markup will look like this:
<div>
<div class="cell">
<h4><%= activity.title %></h4>
<p><%= activity.description %></p>
<span><%= activity.distance %></span>
</div>
<div class="cell">
<%= image_tag(activity.image) %>
</div>
</div>
Is there a way to do this with an if statement? Something like this:
<% if activity.odd? %>
<% #activities.each do |activity| %>
<div>
<div class="cell">
<%= image_tag(activity.image) %>
</div>
<div class="cell">
<h4><%= activity.title %></h4>
<p><%= activity.description %></p>
<span><%= activity.distance %></span>
</div>
</div>
<% end %>
<% else %>
<% #activities.each do |activity| %>
<div>
<div class="cell">
<h4><%= activity.title %></h4>
<p><%= activity.description %></p>
<span><%= activity.distance %></span>
</div>
<div class="cell">
<%= image_tag(activity.image) %>
</div>
</div>
<% end %>
There are a few ways to do this.
The first and best way is to do this with CSS's nth-child selector, assuming it's possible to achieve your desired effect purely through CSS. Use nth-child(odd) or nth-child(even), as described in Alternate table row color using CSS?
If you're iterating over a collection using .each, you can add a .with_index and check whether the index is odd or even:
<% #activities.each.with_index do |activity, index| %>
<% if index.odd? %>
...
<% else %>
...
<% end %>
<% end %>
Note that by default the index is zero-based, so your first element will be considered "even". You can pass a starting index of 1 to with_index if you want the alternating to start odd instead:
<% #activities.each.with_index(1) do |activity, index| %>
An alternative to using with_index is to use Rails' cycle helper, which "magically" returns the next argument in rotation each time its invoked:
<% #activities.each do |activity| %>
<% if cycle('odd', 'even') == 'odd' %>
...
<% end %>
<% end %>
Also note that this is a fantastic opportunity for refactoring. You should distill the two reused cell divs into their own partials and render them in the order you want:
<% if index.odd? %>
<%= render h4_part %>
<%= render img_part %>
<% else %>
<% render img_part %>
<% render h4_part %>
<% end %>

Resources