Question
This is my code. I have a problem with spaces. I can not get the proper generation
- if @lastday.nil? && @lastday != item.created_at.strftime("%d %b %Y")
.daily-entry
%h1.date
=h item.created_at.strftime("%d")
%span
=h item.created_at.strftime("%b, %Y")
-else
.entry
%h1
= link_to item.title, "/items/#{item.id}"
Generates this is
<div class='daily-entry'>
<h1 class='date'>
20
<span>
Sep, 2009
</span>
</h1>
</div>
<div class='entry'>
<h1>
<a href="/items/15">xcvxcvx</a>
</h1>
</div>
But I want a HTML
<div class='daily-entry'>
<h1 class='date'>
20
<span>
Sep, 2009
</span>
</h1>
<div class='entry'>
<h1>
<a href="/items/15">xcvxcvx</a>
</h1>
</div>
</div>
.daily-entry
should be included in the condition
if condition true
<div class='daily-entry'>
<h1 class='date'>
20
<span>
Sep, 2009
</span>
</h1>
<div class='entry'>
<h1>
<a href="/items/15">xcvxcvx</a>
</h1>
</div>
</div>
if condition false
<div class='entry'>
<h1>
<a href="/items/15">xcvxcvx</a>
</h1>
</div>
Solution
Looks like you'll have to duplicate the entry as a first cut:
-if @lastday.nil? && @lastday != item.created_at.strftime("%d %b %Y")
.daily-entry
%h1.date
=item.created_at.strftime("%d")
%span= item.created_at.strftime("%b, %Y")
.entry
%h1= link_to item.title, "/items/#{item.id}"
-else
.entry
%h1= link_to item.title, "/items/#{item.id}"
There ought to be a way to get aound that duplication, but I can't think what it should be - a helper function that takes a block, probably.
OTHER TIPS
Just move the if
within the .daily-entry
:
.daily-entry
- if @lastday.nil? && @lastday != item.created_at.strftime("%d %b %Y")
%h1.date
=h item.created_at.strftime("%d")
%span
=h item.created_at.strftime("%b, %Y")
-else
.entry
%h1
= link_to item.title, "/items/#{item.id}"
Don't you want
if !@lastday.nil?
instead of
if @lastday.nil?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow