Reputation: 553
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
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 usename
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