Chomp .html on Pagination URI's

I’ve been using @tommysundstrom’s optional html with nginx to serve up a site without the .html extension and ran into one place that I’ve been unable to remove the .html extension from the way it’s generated for pagination by middleman. I thought using chomp(’.html’) would work but it didn’t fly. This is what I have:

<ul class="portfolio-nav">
<li><a href="<%= current_article.previous_article.try(:url).chomp('.html') %>">Prev</a></li>
<li><a href="<%= current_article.next_article.try(:url).chomp('.html') %>">Next</a></li>
</ul>

Based on the error I get from middleman I can’t tack on a method to the end of (:url). What would be the proper way to implement this?

try(:url) should return a string, so there should be no problem adding chomp to that – as long as there is an :url to get. Without that chomp has nothing to chomp.

I have not tried your code, but my guess would be that your error comes from the first article (where previous_article probably returns nil, try(:url) will accept this, but will itself return nil.

try documentation: http://api.rubyonrails.org/classes/Object.html#method-i-try

I have not tested it, but current_article.previous_article[:url].chomp('.html') if current_article.previous_article should work.

1 Like

Thank you @tommysundstrom it’s now twice you’ve saved me on this project, thank you.

Just wanted to post here what was used to get pagination working on multiple blogs using the optional_html gem.

<ul>
<li><a href="<%= current_article.previous_article.try(:url).chomp('.html') if current_article.previous_article %>"><i class="icon-chevron-left"></i></a></li>
<li><a href="<%= current_article.next_article.try(:url).chomp('.html') if current_article.next_article %>"><i class="icon-chevron-right"></i></a></li>
</ul>

Thanks for the help @tommysundstrom