Rails div for each block result - ruby-on-rails-3

I have a Rails 3 app where I'm using a block to loop through calls as below:
<div class="blah">
<% #call.each do |call| %>
<%= link_to call.incident_number, call%>
<% end %>
</div>
I want to style the DIV to where it has rounded corners and is a certain size. That's not a problem. But what I've noticed is for each result of the block it continues in the same DIV. I want each result to have it's own div so the results appear separated on the page.
How can I do this using DIV where each result of the block appears in its own DIV?

Just wrap your link_to method in a div content_tag.
<% #call.each do |call| %>
<%= content_tag :div, :class => "some_class" do %>
<%= link_to call.incident_number, call %>
<% end %>
<% end %>

Related

yield is not working in partials other than application.html.erb

Here I have a requirement to use yield in a partial other than application.html.erb , when I try this in partial it was showing me the blank value and if I try the yield in application.html.erb it was showing the value.
application.html.erb
body>
<div id="main-container">
<%= content_for?(:page_header) ? yield(:page_header) : (render :partial => "/shared/home_header") %>
<div id="middle-container">
<%= yield %>
</div>
<div id="bottom-container">
<%= render "/shared/bottom_partial" %>
</div>
index.html.erb
<% content_for :page_header do %>
<%= render :partial => "/shared/employer_header" %>
<% end %>
<% content_for :page_name do %>
<p>My Account</p>
<% end %>
<div>
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx
</div>
/shared/_employer_header.html.erb
<div class="heading" >
<%= yield(:page_name) %>
</div>
When I am trying like above I am getting blank value in yield :page_name in employer header if I try the yield :page name in application.html.erb I am getting the value.Can any one help me to sort this out.Thank'U'.
Swap the order of your two content_fors so the :page_name is defined before the partial is rendered.
index.html.erb
<% content_for :page_name do %>
<p>My Account</p>
<% end %>
<% content_for :page_header do %>
<%= render :partial => "/shared/employer_header" %>
<% end %>

css: how to display two products in one row using each loop (rails)

Need some help in css.
i have an array of products and m displaying it using each loop. I wanna show two products in each row. do you know how to do it? currently i am using 50% width of div so two products will come in div.
Is there any other way?
Here is code:
<div id="product_list" >
<% if #products.size <= 0 %>
<h1/>No products found</h1>
<% else %>
<% #products.each do |p| %>
<div class="products">
<div><%= image_tag p.photos.first.avatar.url(:big) if p.photos.size > 0 %>
</div>
</div>
<% end %>
<% end %>
</div>
div product_list is main div
div products is 50%.
So it displays 2 div under main.
But this behaves weird when filters based on category etc. Is there any good way for this?
You can use
I many times handle such situation like this, try if it works for you.
pseudo code:
<div id="product_list" >
<% if #products.size <= 0 %>
<h1/>No products found</h1>
<% else %>
<ul><li class="products">
<% #products.each do |p| %>
<%= (count%2==0):'</li><li>':''; %>
<div><%= image_tag p.photos.first.avatar.url(:big) if p.photos.size > 0 %>
</div>
<% end %>
</li></ul>
<% end %>
</div>

Determining if a block has any content when passed to a partial

<%= render :layout => "some_layout" do %>
<p>Some stuff</p>
<% end %>
In "some_layout"
<%= yield %>
Is there a way to detect if the yield block has any content? Example:
<% if block_has_content? %>
<%= yield %>
<% else %>
<p>Default content</p>
<% end %>
I think you should have a look to this documentation:
http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for
You can define somewhere:
<% content_for :some_layout do %>
some content
<% end %>
And in an other part of your code:
<%= content_for(:some_layout) || 'Default content' %>
If content for :some_layout is not defined, it will take the default one.

rails bootstrap icon in a link to

I'm trying to use one of the icons in Bootstrap for a link_to.
These don't work::
<% link_to '<i class="icon-pencil"></i>', task_path(task)%>
<% link_to task_path(task), html_options = {:class => 'icon-pencil'} %>
I would appreciate the help!
You can use this other form of the link_to helper:
<%= link_to task_path(task) do %>
<i class="icon-pencil"></i>
<% end %>
Make sure to include the = sign before the link_to call.

Is it possible to pass html elements into a partial?

I have a partial that contains a header, a subheader and potentially one or more buttons. It is used on many different pages. Often the pages don't need buttons, so I just pass in the header and an optional subheader to the partial. However sometimes the pages need one or more buttons and the only way I've managed to allow for an arbitrary number of buttons to be passed in is using content_for. My partial looks like this:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= content_for :page_header_buttons %>
</ul>
</header>
<% end %>
This use of content_for nasty. Is there any way I can pass the list items / buttons into this partial? How else could I deal with this situation?
You could transform this partial into a layout:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= yield %>
</ul>
</header>
<% end %>
And you would render it like that:
<%= render layout: "your_partial_above", locals: { page_title: "page title } do %>
<%= render "partial_with_your_buttons" %>
<% end %>