How to define static variable across a partial?

Hi,

I have partial:
<%=render(:partial => ‘blog_post’, :collection => @blogs)%>

This outputs a series of blog posts, however I only want to output the
created_on date if it is different from the last, is there a way to
compare these or create a static variable that I can assign the date to?

josh wrote:

Hi,

I have partial:
<%=render(:partial => ‘blog_post’, :collection => @blogs)%>

This outputs a series of blog posts, however I only want to output the
created_on date if it is different from the last, is there a way to
compare these or create a static variable that I can assign the date to?

You’re saying the @blogs array is already sorted by the created_on date
?

Then you can expand the loop

<% last_date = nil %>

<% @blogs.each do |blog| %>
<%= render :partial => ‘blog_post’, blog,
:locals => { :ignore_date => (last_date == blog.created_on)
} %>

<% last_date = blog.created_on %>

<% end %>

Then change the blog_post partial to not show the created_on date if
variable “ignore_date” has value true.

Stephan

I’m not sure if this is what you’re asking but this is what i’ve
implemented in a partial called _newest_date.rhtml :

<% if newest_date.created_at == newest_date.updated_at %>
<%= newest_date.created_at.strftime(“Created on %m/%d/%Y”) %>
<%= newest_date.created_at.strftime(“at %I:%M%p”) %>
<% else %>
<%= newest_date.updated_at.strftime(“Updated on %m/%d/%Y”) %>
<%= newest_date.updated_at.strftime(“at %I:%M%p”) %>
<% end %>

And i call this for every post that is shown in a page. With a little
more detail the list.rhtml file :

<% for post in @posts %>

<%= link_to h(post.title), :action => 'edit', :id => post %>
<ul class="post_info">
  <%= render :partial => 'newest_date', :object => post %>
</ul>

[.... stuff here.....]

<% end %>

You could call a partial like this one from inside another partial.

I’m pretty sure this is NOT the best way to do it, but i’m pretty new
at rails so this is a fast solution i found for my problem, which in
this case is to either print the created_at date or the updated_at
date according to which one is the newest.

P.S.: The controller just returns the @posts collection through a
paginator :

def list
@post_pages, @posts = paginate :posts, :order => ‘id
DESC’, :per_page => 10
end

–Hope i helped a little

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs