4

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.

krnjn
  • 125
  • 1
  • 3
  • 8

2 Answers2

5

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') %>
Stefan
  • 109,145
  • 14
  • 143
  • 218
0

Try this:

<%= link_to "Leave a comment", [post.postable, post], :anchor=> 'new-comment' %>
Rahul garg
  • 9,202
  • 5
  • 34
  • 67