user2759575
user2759575

Reputation: 553

link_to specific post using anchor

After a user posts a question i want to redirect to where its posted and scroll down to the bottom of the page where the question is, using an anchor. It seems like i'm close but the page still doesn't move. This is the link i have:

if @question.save
redirect_to post_comment_path(@comment.post, @comment) + "#question_#{@question.id.to_s}"
end 

Also, the link is showing the proper id at the end like so:

posts/1/comments/1#question_1

Upvotes: 1

Views: 114

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

As per the comments, you need to tell HTML where to "scroll" to -

This is typically done with the id attribute of HTML elements, but HTML5 now allows you to use name attribute to achieve the same thing

Bottom line is if you're appending an anchor reference to your url, the only way a browser will be able to access it will be with the id or name element of your page. I would do this:

#app/views/controller/index.html.erb
<%= @collection.each do |comment| %>
    <%= content_tag :div, class: "class", id: "question_#{comment.id}" %>
<% end %>

This will allow you to use the comment id directly when you send the url:

#question_#{@question.id.to_s}

Upvotes: 1

Related Questions