title: How to create an anchor and redirect to this specific anchor in Ruby on Rails
body: <p>I'm trying to create unique anchors for every comment on my blog so a person can take the url of an anchor and paste it in their browser, which will automatically load the page and scroll down to the point in the page where their comment starts. </p>
  
  <p>Perhaps I'm going about this the wrong way but I've tried this which was to no avail.</p>
  
  <p>Comment view - Fail 1 - when pasted in a browser this link does not scroll down to the desired position</p>
  
  <pre><code>&lt;%= link_to '#', :controller =&gt; 'posts', :action =&gt; 'show', :id =&gt; comment.post, :anchor =&gt; 'comment_' &lt;&lt; comment.id.to_s %&gt;
  </code></pre>
  
  <p>Comments controller - Fail 2 - Correct url in browser but no scrolling happens it just stays at the top of the page</p>
  
  <pre><code>redirect_to :controller =&gt; 'posts', :action =&gt; 'show', :id =&gt; @post, :anchor =&gt; 'comment_' + @comment.id.to_s
  </code></pre>
  
  <p>If someone could help I'd be very grateful :)</p>
  
  <p>UPDATE: The solutions below almost work, however I come out with the following URL which isn't being scrolled to if I click on it. </p>
  
  <p># 
  i.e. <a href="http://localhost:3000/posts/please-work" rel="noreferrer">http://localhost:3000/posts/please-work</a></p>