替代路由助手方法 [英] Override route helper methods

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

问题描述

问题有很多评论.

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

A URL "questions/123" shows a question.

URL:

"questions/123#answer-345"

"questions/123#answer-345"

显示问题并突出显示答案. 345-是Answer模型的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

"questions/123#answer-345"

"questions/123#answer-345"

代替

"answers/345"

"answers/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天全站免登陆