As I said in my comment, methods ending by ?
should return a boolean value. If you decide to go against the convention this will make things harder for us.
I suggest you actually use an active_link_to
like it is explained in that question.
However the main problem was that you didn't generate the URL properly for each post :
is_active?(posts_path)
The posts_path
is the path to the index and not the individual post resource. You should use something like post_path(post)
You want to do something like this :
First your is_active?
method, because it has a ? should return a boolean
def is_active?(path)
current_page?(path)
end
Then you can use it this way (you need to get the URL of the post using the post_path(post)
helper)
<% @posts.each do |post| %>
<%= link_to post.title, post, class: ('active' if is_active?(post_path(post))) %>
<% end %>
EDIT : because is_active? does the same thing as current_page? you should simply replace the is_active?
code by an alias declaration
alias :is_active? current_page?