how to "Each do |article|" but after every n-articles it has to start in a new row(foundation) - ruby

I got this Rails blogs app and for the indexpage where the articles are posted it shows a short version of the article.
<%= render 'layouts/header' %>
<%= render 'layouts/topbar' %>
<div class="contentnews">
<%= render 'layouts/bannerad' %>
<div class="row">
<% if current_user.try(:admin?) %>
<%= link_to 'Nieuws toevoegen', new_article_path, :class => "button ala" %>
<% end %>
</div>
<div class="row">
<%= render 'overview' %>
</div>
</div>
The overview-partial :
<div class="articlebox ">
<div class="large-4 columns">
<% #articles.reverse_each do |article| %>
<div class="articlebox2">
<div class="articleindex">
<%= link_to article.title, article %>
</div>
<div class="articlebody">
<%= article.short.to_s.html_safe %>
</div>
</div>
<% end %>
</div>
</div>
The problem is that this way all the articles end up in one row. I want it to show the short articles inline on the page and after every two or three article it has to start in a new row with again two or three short articles.
Anybody got any idea?

You don't actually need to create a row every n-elements. With foundation just wrap all the elements in a row, and give them columns class:
<div class="articlebox">
<div class="large-4 columns">
<div class="row"> <!-- Wrap in a row -->
<% #articles.reverse_each do |article| %>
<div class="articlebox2 large-6 columns"> <!-- Give a column and width class here -->
...
</div>
<% end %>
</div>
So you can nesting grids with foundation, and creating new rows is not necessary, foundation will just wrap if the elements exceeds the grid.
If you actually really want to create rows, you can have a look at the method each_with_index API and then insert row elements if index % 2 == 0. But this is unnecessary complexity.

Related

bootstrap grid layout from left to right column

i have been experimenting with rails 6 and I'm stuck regarding a certain bootstrap grid placement. i want to create something in the lines of a photo blog. meaning people can upload photos or text and it should get displayed in a grid system filling 4 columns and then moving down from left to right till eventually reach like 5 rows and then code in pagination (but that's for another time) .
So far ive made a grid and can display anything uploaded to the databse but my code fills only the first row and not moving from left to right. any suggestions?
<tbody>
<% #ads.each do |ad| %>
<tr>
<td><%= ad.title %></td>
<td><%= ad.description %></td>
<td><%= ad.area %></td>
<td><%= ad.contact %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Ad', new_ad_path %>
<div class="container">
<% #ads.each do |ad| %>
<div class="row mt-3">
<div class="col-sm">
<div class="card">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title"> <%= ad.title %> </h5>
<p class="card-text"><%= ad.description %></p>
<p class="card-text"><%= ad.area %></p>
<%= ad.contact %>
</div>
</div>
</div>
<div class="col-sm">
One of three columns
</div>
<div class="col-sm">
One of three columns
</div>
</div>
<% end %>
</div>
You can use Enumerable#each_slice in the following way.
This example is pure ruby, but you can easily transpose to rails view with proper erb and html markups:
ads = (11..24) # emulates your collection
items_per_row = 5
ads.each_slice(items_per_row) do |row_items|
# start a row: <div class="row mt-3">
row_items.each do |item|
# start a columnn: <div class="col-sm">
print "| #{item}" # only for this demo, add your item with markup here
# close the column: </ div>
end
# close the row: </ div>
puts # only for this demo, remove in erb
end
The above code as pure Ruby returns:
# | 11| 12| 13| 14| 15
# | 16| 17| 18| 19| 20
# | 21| 22| 23| 24

Spree: Deface getting error NameError in Spree::Home#index

I'm trying to change the look of the front page where the items are listed but I'm getting an error. Am I using deface correctly to change it? Its telling me that <%= link_to small_image(product, itemprop: "image"), url, itemprop: 'url' %> is giving me errors, if I removed then there are no pictures. What do I do?
update_products.rb
Deface::Override.new(:virtual_path =>"spree/shared/_products",
:name => "change site",
:replace =>"#products",
:text => '
<%
paginated_products = #searcher.retrieve_products if params.key?(:keywords)
paginated_products ||= products
%>
<% content_for :head do %>
<% if paginated_products.respond_to?(:num_pages) %>
<%= rel_next_prev_link_tags paginated_products %>
<% end %>
<% end %>
<div data-hook="products_search_results_heading">
<% if products.empty? %>
<div data-hook="products_search_results_heading_no_results_found">
<%= Spree.t(:no_products_found) %>
</div>
<% elsif params.key?(:keywords) %>
<div data-hook="products_search_results_heading_results_found">
<h6 class="search-results-title"><%= Spree.t(:search_results, keywords: h(params[:keywords])) %></h6>
</div>
<% end %>
</div>
<% if products.any? %>
<div id="products" class="row" data-hook>
<% products.each do |product| %>
<% url = spree.product_url(product, taxon_id: #taxon.try(:id)) %>
<div id="product_<%= product.id %>" class="col-md-3 col-sm-6 product-list-item" data-hook="products_list_item" itemscope itemtype="https://schema.org/Product">
<div class="panel panel-default">
<% cache(#taxon.present? ? [I18n.locale, current_currency, #taxon, product] : [I18n.locale, current_currency, product]) do %>
<div class="panel-body text-center product-body">
<br/>
<%= link_to small_image(product, itemprop: "image"), url, itemprop: 'url' %>
</div>
<div class="panel-footer text-center">
<span itemprop="offers" itemscope itemtype="https://schema.org/Offer">
<span class="price selling lead" itemprop="price"><%= display_price(product) %></span>
</span>
</div>
<% end %>
</div>
</div>
<% end %>
<% reset_cycle("classes") %>
</div>
<% end %>
<% if paginated_products.respond_to?(:num_pages) %>
<% end %>
')
one small quote issue for url <%= link_to small_image(product, itemprop: "image"), url, itemprop: "url" %> check this code :-
Deface::Override.new(:virtual_path =>"spree/shared/_products",
:name => "change site",
:replace =>"#products",
:text => '
<%
paginated_products = #searcher.retrieve_products if params.key?(:keywords)
paginated_products ||= products
%>
<% content_for :head do %>
<% if paginated_products.respond_to?(:num_pages) %>
<%= rel_next_prev_link_tags paginated_products %>
<% end %>
<% end %>
<div data-hook="products_search_results_heading">
<% if products.empty? %>
<div data-hook="products_search_results_heading_no_results_found">
<%= Spree.t(:no_products_found) %>
</div>
<% elsif params.key?(:keywords) %>
<div data-hook="products_search_results_heading_results_found">
<h6 class="search-results-title"><%= Spree.t(:search_results, keywords: h(params[:keywords])) %></h6>
</div>
<% end %>
</div>
<% if products.any? %>
<div id="products" class="row" data-hook>
<% products.each do |product| %>
<% url = spree.product_url(product, taxon_id: #taxon.try(:id)) %>
<div id="product_<%= product.id %>" class="col-md-3 col-sm-6 product-list-item" data-hook="products_list_item" itemscope itemtype="https://schema.org/Product">
<div class="panel panel-default">
<% cache(#taxon.present? ? [I18n.locale, current_currency, #taxon, product] : [I18n.locale, current_currency, product]) do %>
<div class="panel-body text-center product-body">
<br/>
<%= link_to small_image(product, itemprop: "image"), url, itemprop: "url" %>
</div>
<div class="panel-footer text-center">
<span itemprop="offers" itemscope itemtype="https://schema.org/Offer">
<span class="price selling lead" itemprop="price"><%= display_price(product) %></span>
</span>
</div>
<% end %>
</div>
</div>
<% end %>
<% reset_cycle("classes") %>
</div>
<% end %>
<% if paginated_products.respond_to?(:num_pages) %>
<% end %>
')

Values are not showing using Rails 3

I want to display data inside text field and those values will be fetched dynamically.But i am not getting any value(only blank filed is coming) when show action is executing.
Please check mu code given below.
Views/posts/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<input type="text",value="<%= #post.name %>">
</p>
<p>
<b>Title:</b>
<input type="text",value="<%= #post.title %>">
</p>
<p>
<b>Content:</b>
<input type="text",value="<%= #post.content %>">
</p>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%= link_to 'Back', posts_path %>
Please help me to resolve this issue.
Replace the value Property by removing quotes as:
<input type="text",value=<%= #post.content %>>

Creating Gallery Page/Nested Resource

I am trying to create a gallery page that links to photo albums. The albums work fine, but I am trying to pull the first image from each gallery_id to the gallery page. I have a Gallery has many photos, and photos belong to gallery. What I am getting is the first image loading for each album.
class GalleriesController < ApplicationController
def index
#gallery = Gallery.paginate(page: params[:page]).per_page(6)
#photos = Photo.find(:all, :limit => 1)
end
def show
#gallery = Gallery.find(params[:id])
#photos= #gallery.photos.all
end
end
galleries/index.html.
<% provide(:title, 'Photo Galleries') %>.
<div id="galleries">
<%= will_paginate #gallery %>
<ul class="thumbnails">
<% #gallery.each do |gallery| %>
<li class="span4">
<div class="thumbnail">
<% #photos.each do |photo| %>
<%= link_to image_tag(photo.image), gallery_path(gallery)%>
<% end %>
<h4><%= gallery.name %></h4>
</div>
</li>
<% end %>
</ul>
</div>
routes
resources :galleries, :has_many => :photos
any help would be appreciated.
Pretty sure this is what you want:
class GalleriesController < ApplicationController
def index
#gallery = Gallery.paginate(page: params[:page]).per_page(6)
end
end
_
# galleries/index.html
<% provide(:title, 'Photo Galleries') %>
<div id="galleries">
<%= will_paginate #gallery %>
<ul class="thumbnails">
<% #gallery.each do |gallery| %>
<li class="span4">
<div class="thumbnail">
<%= link_to image_tag(gallery.photos.first.image), gallery_path(gallery) %>
<h4><%= gallery.name %></h4>
</div>
</li>
<% end %>
</ul>
</div>
The problem in yours is that in your index action you grabbed ALL the images, regardless of what gallery they belong to, and then you looped through all of them in your view and displayed them.
Because of your association (gallery has_many photos), you can access a gallery's photos with gallery.photos.
In my example, I am displaying the first image for each gallery: gallery.photos.first
If you want a random image from the gallery in question you can use sample. i.e. gallery.photos.sample
You have to use relations, which is what you need here. I tried to fix the code and added comments.
class GalleriesController < ApplicationController
def index
#gallery = Gallery.paginate(page: params[:page]).per_page(6)
# You don't need the photos. You have to access them through gallery,
# or you will get always all photos independent of the gallery.
##photos = Photo.find(:all, :limit => 1)
end
This is the view you are looking for
# galleries/index.html.erb
<% provide(:title, 'Photo Galleries') %>.
<div id="galleries">
<%= will_paginate #gallery %>
<ul class="thumbnails">
<% #gallery.each do |gallery| %>
<li class="span4">
<div class="thumbnail">
<% gallery.photos.each do |photo| %>
<%= link_to image_tag(photo.image), gallery_path(gallery)%>
<% end %>
<h4><%= gallery.name %></h4>
</div>
</li>
<% end %>
</ul>
</div>
If you only want to show the first picture of every gallery, you have to change the view this way:
# galleries/index.html.erb
<% provide(:title, 'Photo Galleries') %>.
<div id="galleries">
<%= will_paginate #gallery %>
<ul class="thumbnails">
<% #gallery.each do |gallery| %>
<li class="span4">
<div class="thumbnail">
<%= link_to image_tag(gallery.photos.first.image), gallery_path(gallery)%>
<h4><%= gallery.name %></h4>
</div>
</li>
<% end %>
</ul>
</div>

Issue in kaminari ajax pagination

Am using kaminari ajax pagination in my project. It is working fine, But contents are displaying number of pages times in the page. For example if number of items per pages is 7, then it is displaying 7 times the same content. What am doing is
In product_details controller
def index
#products=ProductDetail.where("department_id = ? and category_id = ?",1, 1).page(params[:page]).per(15)
end
In product_details/index.html.erb
<div id="product_details">
<%= render #products %>
</div>
<div id="paginator">
<%= paginate #products, :remote=>true %>
</div>
In product_details/index.js.erb
$('#product_details').html('<%= escape_javascript render (#products) %>');
$('#paginator').html('<%= escape_javascript(paginate(#products, :remote=>true).to_s)%>');
In product_details/_product_detail.html.erb
<div id="product_list">
<% #products.each do | product | %>
<div class="product_container">
<div class="product_box" >
<div id="future_image_container">
<div class="image_block" >
<%= image_tag(product.image_path, :alt => "product image", :height=>"215px") %>
</div>
<span id="future_price" style="text-decoration: line-through; color: #9e9c9c;">
<span style="color: black;">
<%= "$#{product.price}" %>
</span>
</span>
<div id="circle">
<p>
<% if(product.discount > 0) %>
<% new_price=((2.0*((product.price*product.discount)/100)).round)/2.0 %>
<% else %>
<% new_price=product.price %>
<% end %>
<%= "$#{new_price}"%>
</p>
</div>
</div>
<p class="future_product_name">
<%= product.name %>
</p>
<% #brands=Brand.where("id=?",product.brand_id)
#brands.each do |brand|
#brandname=brand.name
end
%>
<p class="future_product_name">
<%= "Brand : #{#brandname}" %>
</p>
</div>
</div>
<% end %>
</div>
Please help me to solve this problem
I noticed that when i use <%= render #products %> how many items i have taken per page that number of times it repeates. Therefore I solved this by the following code:
In product_details controller
def index
#products=ProductDetail.where("department_id = ? and category_id = ?",1, 1).page(params[:page]).per(15)
end
In product_details/home.html.erb
<div id="product_details">
<%= render 'index' %>
</div>
In product_details/index.js.erb
$('#product_details').html('<%= escape_javascript render ('index') %>');
In product_details/_index.html.erb
<div id="product_list">
<%= paginate #products, :remote=>true %>
<% #products.each do | product | %>
<div class="product_container">
<div class="product_box" >
<div id="future_image_container">
<div class="image_block" >
<%= image_tag(product.image_path, :alt => "product image", :height=>"215px") %>
</div>
<span id="future_price" style="text-decoration: line-through; color: #9e9c9c;">
<span style="color: black;">
<%= "$#{product.price}" %>
</span>
</span>
<div id="circle">
<p>
<% if(product.discount > 0) %>
<% new_price=((2.0*((product.price*product.discount)/100)).round)/2.0 %>
<% else %>
<% new_price=product.price %>
<% end %>
<%= "$#{new_price}"%>
</p>
</div>
</div>
<p class="future_product_name">
<%= product.name %>
</p>
<% #brands=Brand.where("id=?",product.brand_id)
#brands.each do |brand|
#brandname=brand.name
end
%>
<p class="future_product_name">
<%= "Brand : #{#brandname}" %>
</p>
</div>
</div>
<% end %>
</div>
Now no product repeates and ajax pagination is also working fine

Resources