Add anchor option in Rails link_to using array

anchor, erb, link-to, ruby-on-rails

Solution

You can call `polymorphic_path`:

<%= link_to pluralize(post.comments.count, 'comment'), polymorphic_path([post.postable, post], anchor: 'comments') %>

<%= link_to "Leave a comment", polymorphic_path([post.postable, post], anchor: 'new-comment') %>

Problem

I'm using an array to generate the path for my rails link_to tags and can't seem to figure out how to add an anchor option. Here are my link_to tags: ``` <%= link_to pluralize(post.comments.count, 'comment'), [post.postable, post] %> <%= link_to "Leave a comment", [post.postable, post] %> ``` Since I'm using a polymorphic association for the posts (and they are nested routes), I can't simply use the paths generated by the resources helpers in the routes.rb file. Previously, I was able to use the anchor option on the paths automagically generated as I was not using the polymorphic association with this model. This is what that looked like: ``` <%= link_to pluralize(post.comments.count, 'comment'), project_post_path(@project, post, {anchor: 'comments'}) %> <%= link_to "Leave a comment", project_post_path(@project, post, {anchor: 'new-comment'}) %> ``` Any tips on how to get an anchor tag back into the link_to tags when using an array to generate the url? Thanks in advance.

Original source