1

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
user2759575
  • 553
  • 3
  • 25

1 Answers1

1

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}
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • Ok, bare with me. @questions.each displays <%= question.body %>. Do i add the whole content_tag line or just attach the id inside <%= question.body %>? – user2759575 Apr 28 '14 at 19:54