2

I've tried

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

But ID only seems to modify the href, resulting in

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

How do I set the ID correctly? Also can someone explain to me why :id is modifying the href just so I can understand what's going on behind the scenes?

4

3 回答 3

8

你必须放在:action => 'bar'一个哈希里面。

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

文档中有一个示例,该示例针对您的相同问题举了一个示例:

CSS 的类和 id 很容易生成:

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

使用较旧的参数样式时要小心,因为需要额外的文字哈希:

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

离开散列会给出错误的链接:

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

这就是为什么你最好使用新的参数样式,为路由加上别名,而不是显式调用控制器和动作。

于 2013-05-09T23:09:44.930 回答
2

link_to您可以使用标签传递参数,例如

= link_to "foo", { :controller => "foo", :id => "foobar"}

或者

= link_to "foo", {:action=> "any-action, ":controller => "foo", :id => "foobar"}

在这里你不能使用链接_tag的参数

于 2013-05-10T10:37:55.993 回答
2

你可以为此使用路径助手吗?这将取决于您的路线配置是否正确。

例如:

= link_to "foo", foo_path, :id => "foobar"

于 2013-05-09T23:10:30.720 回答