gpt4 book ai didi

ruby-on-rails - 如何在 haml 的 link_to 标签中设置 ID?

转载 作者:行者123 更新时间:2023-12-04 03:54:33 25 4
gpt4 key购买 nike

我试过了

= link_to 'foo', :action => 'bar', :id => 'foobar'

但是id好像只是修改了href,导致

<a href="controller/foobar/bar">foo</a>

如何正确设置ID?也有人可以向我解释为什么 :id 正在修改 href 只是为了让我了解幕后发生的事情吗?

最佳答案

您必须将 :action => 'bar' 放入散列中。

= link_to 'foo', { :action => 'bar' }, :id => 'foobar'

docs 中有一个示例用你同样的问题举例说明:

Classes and ids for CSS are easy to produce:

link_to "Articles", articles_path, :id => "news", :class => "article"
# => <a href="/articles" class="article" id="news">Articles</a>

Be careful when using the older argument style, as an extra literal hash is needed:

link_to "Articles", { :controller => "articles" }, :id => "news", 
:class => "article"
# => <a href="/articles" class="article" id="news">Articles</a>

Leaving the hash off gives the wrong link:

link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# => <a href="/articles/index/news?class=article">WRONG!</a>

这就是为什么您最好使用新的参数样式,为路由设置别名,而不是显式调用 Controller 和操作。

关于ruby-on-rails - 如何在 haml 的 link_to 标签中设置 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16472499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com