I have a posts view that contains links to the adjacent posts. I've added a class in posts_helper to decide what the links should be.
show.html.erb:
<ul>
<% links = PostsHelper::Links.new(@post) %>
<li><%= links.older %></li>
<li><%= links.newer %></li>
<li><%= link_to 'show all', posts_path %></li>
</ul>
posts_helper.rb:
module PostsHelper
class Links
def initialize(post)
@post = post
end
def newer
link_to 'newer', post_path(newer_post)
end
def older
link_to 'older', post_path(older_post)
end
private
attr_reader :post
def newer_post
Post.where(['id > ?', post.id]).last
end
def older_post
Post.where(['id < ?', post.id]).first
end
end
end
Routes:
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
post GET /posts/:id(.:format) posts#show
But I can't access post_path
within the helper class. I get an error:
undefined method `post_path' for #<PostsHelper::Links:0x007fb884ad10d0>
This makes me think that I'm not going about things the 'rails' way.
How do I go about using the posts_path
helper? Or have I veered too far from the ideal method?