覆盖路由助手方法

Ant*_*nAL 14 routing overriding ruby-on-rails

问题有很多评论.

URL"questions/123"显示一个问题.

一个网址:

"问题/ 123#答案-345"

显示一个问题并突出显示答案.345-是Answer模型的id,"answer-345"是HTML元素的id属性.

我需要覆盖"answer_path(a)"方法来获取

"问题/ 123#答案-345"

代替

"答案/ 345"

怎么做 ?

rub*_*iii 16

所有url和path helper方法都接受可选参数.
您正在寻找的是这个anchor论点:

question_path(123, :anchor => "answer-345")
Run Code Online (Sandbox Code Playgroud)

它记录在URLHelper#link_to示例中.

使用此参数,您应该能够answer_path通过以下方式创建帮助程序:

module ApplicationHelper

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

end
Run Code Online (Sandbox Code Playgroud)