覆盖路由辅助方法 [英] Override route helper methods

查看:18
本文介绍了覆盖路由辅助方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题有很多评论.

网址questions/123"显示了一个问题.

A URL "questions/123" shows a question.

网址:

问题/123#answer-345"

"questions/123#answer-345"

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

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

我需要重写answer_path(a)"方法来获取

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

问题/123#answer-345"

"questions/123#answer-345"

代替

答案/345"

怎么做?

推荐答案

所有 url 和 path 辅助方法都接受可选参数.
您正在寻找的是 anchor 参数:

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

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

它记录在 URLHelper#link_to 示例.

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

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

这篇关于覆盖路由辅助方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆