Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override route helper methods

Question has many Comments.

A URL "questions/123" shows a question.

A URL:

"questions/123#answer-345"

shows a question and highlights an answer. 345 - is id of Answer model, "answer-345" is id attribute of HTML element.

I need to override "answer_path(a)" method to get

"questions/123#answer-345"

instead of

"answers/345"

How to do it ?

like image 260
AntonAL Avatar asked Mar 05 '11 12:03

AntonAL


1 Answers

All url and path helper methods accept optional arguments.
What you're looking for is the anchor argument:

question_path(123, :anchor => "answer-345")

It's documented in the URLHelper#link_to examples.

Using this argument, you should be able to create an answer_path helper via:

module ApplicationHelper

  def answer_path(answer)
    question_path(answer.question, :anchor => "answer-#{answer.id}")
  end

end
like image 71
rubiii Avatar answered Nov 14 '22 17:11

rubiii