Add binding option in Rails link_to using array

I am using an array to generate the path for rails link_to links and cannot figure out how to add a binding parameter. 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 use polymorphic association for messages (and they are nested routes), I cannot just use the paths created by the resource helpers in the routes.rb file.

Previously, I could use the anchor option on paths automatically generated, since I did not use a polymorphic relationship with this model. Here's how it looked:

 <%= 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 return the anchor tag back to link_to tags when using an array to generate a url? Thanks in advance.

+4
source share
2 answers

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') %> 
+4
source

Try the following:

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

Source: https://habr.com/ru/post/1443509/


All Articles