Blogging helper next/previous link issue

141 views
Skip to first unread message

Sven Kosma

unread,
Feb 7, 2012, 3:44:43 PM2/7/12
to nanoc
Hi,

I'm trying to set up links to the next and previous article, and I'm
at my wits end... here's what I have:

prv = sorted_articles.index(@item) - 1
link_to '← Previous', sorted_articles[prv].reps.find { |r| r.name
== :default }, :class => "prev nav"

nxt = sorted_articles.index(@item) + 1
link_to 'Next →', sorted_articles[nxt].reps.find { |r| r.name
== :default }, :class => "next nav"

The previous link is generated without an issue, but the next link
causes this error:
undefined method `reps' for nil:NilClass

I don't really understand what's going on... if I change the "+1" in
nxt = sorted_articles.index(@item) + 1 to "+0" I get the link to the
current article, which I would expect, and if I substitute the nxt
variable in sorted_articles[nxt].reps.find with the actual value of
sorted_articles.index(@item) + 1, say 5 if the index of the current
article is 4, I get a proper link. Any ideas?

-Sven

Denis Defreyne

unread,
Feb 7, 2012, 4:00:30 PM2/7/12
to na...@googlegroups.com
Hi Sven,

It looks like you are not taking into account that some articles don’t have a next or previous (first and last one). So, sorted_articles[prv] and sorted_articles[nxt] will be nil in those cases.

Denis

> --
> You received this message because you are subscribed to the nanoc discusssion group.
>
> To post to this group, send email to na...@googlegroups.com
> To unsubscribe from this group, send email to
> nanoc+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nanoc?hl=en

Message has been deleted

Panagiotis Galatis

unread,
Sep 1, 2012, 10:32:56 AM9/1/12
to nanoc
My bad. I thought I had solved it but I'm back to square 1 :-(

On Sep 1, 4:16 pm, Panagiotis Galatis <pgala...@gmail.com> wrote:
> Ok, solved :-)
>
> Went into /lib/default.rb and defined the next and previous
> links separately like so:
>
>  def previous_link
>     prv = sorted_articles.index(@item) - 1
>     return link_to('&larr; Previous', sorted_articles[prv].reps.find { |r|
> r.name == :default }, :class => "prev nav") unless
> sorted_articles[prv].reps.find.nil?
>   end
>
>   def next_link
>     nxt = sorted_articles.index(@item) + 0
>     return link_to('&rarr; Next', sorted_articles[nxt].reps.find { |r|
> r.name == :default }, :class => "next nav") unless
> sorted_articles[nxt].reps.find.nil?
>   end
>
> I then went to my post layout template and called them. That was all :-)

Panagiotis Galatis

unread,
Sep 1, 2012, 9:05:47 PM9/1/12
to na...@googlegroups.com, sven....@gmail.com
Fixed as follows (finally!)

In /lib/default.rb

def previous_link
    prv = sorted_articles.index(@item) - 1 
    link_to('&larr; Previous', sorted_articles[prv].reps[0], :class => "previous")
end

def next_link
    nxt = sorted_articles.index(@item) + 1
    if sorted_articles[nxt].nil?
      return '(none)'
    else
    link_to('Next &rarr;', sorted_articles[nxt].reps.find { |r| r.name == :default }, :class => "next")
  end
end  

It works fine. You can see it in action at http://panosgalatis.com with a twist of Twitter Bootstrap.

On Tuesday, February 7, 2012 10:44:43 PM UTC+2, Sven Kosma wrote:
Reply all
Reply to author
Forward
0 new messages