I am using a link_to tag on my posts/index view and want to link it to my posts/show/id view with an anchor that makes it scroll down to the comments form. For some reason I can't get the anchor to work. Here is my code:
In posts/index
<%= link_to 'Add a Comment', post, :anchor => 'comment_form' %>
This fails to append the # sign to the end of the link, so it is just localhost:3000/posts/id. I have also tried many variations for link_to, including:
<%= link_to 'Add a Comment', post(:anchor => 'comment_form' %>
and
<%= link_to 'Add a Comment', :controller => 'posts', :action => 'show', :id => @post, :anchor => 'comment_form' %>
but I've had no luck.
Here is my posts#show action:
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
and here is the posts/show view where I want the anchor to scroll to:
<h2><a name="comment_form" id="comment_form">Add a comment:</a></h2>
Furthermore, any of the above works if I am linking to something on the index page, as I can see the hash # has been appended to the outputted url. For some reason it is not working when trying to link to the show page. Any help with this?