[英]Rails+Slim: How to use link_to tag with controller & action name; and nested content in the anchor tag
This is what I have right now: 这就是我现在所拥有的:
li
= link_to {:controller => "controllername", :action => "actionname"}
span.glyphicon.sidebar-icon.glyphicon-ok-circle
span WhoHas
However this won't work as link_to
expects it's first argument to be the content inside the anchor tag. 但是,这将无法正常工作,因为
link_to
期望它的第一个参数是anchor标记内的内容。 The contents I'd like inside the anchor tag are the following span
blocks. 我想要的锚标记内的内容是以下
span
块。
Any help would be much appreciated! 任何帮助将非常感激!
You said this: 你这样说:
However this won't work as link_to expects it's first argument to be the content inside the anchor tag.
但是,这将无法正常工作,因为link_to期望它的第一个参数是anchor标记内的内容。
This is not true. 这不是真的。
link_to
can also accept a block instead, and it will use the content of the block instead: link_to
也可以接受一个块,它将使用该块的内容:
link_to("/path/to/thing") do
"My link"
end
And, as such, does not require the first argument to be used as the text for the link. 并且,因此,不需要将第一个参数用作链接的文本。 The documentation list the following signatures:
该文档列出了以下签名:
link_to(body, url, html_options = {})
# url is a String; you can use URL helpers like
# posts_path
link_to(body, url_options = {}, html_options = {})
# url_options, except :method, is passed to url_for
link_to(options = {}, html_options = {}) do
# name
end
link_to(url, html_options = {}) do
# name
end
The last two options are what you're looking for. 最后两个选项是您要寻找的。
However, the real reason your code doesn't work is because you're using braces without parentheses - basically, method { :hello => "world" }
doesn't do what you think it does. 但是,代码不起作用的真正原因是因为您使用的括号不带括号-基本上,
method { :hello => "world" }
并没有达到您的预期。 the method { }
syntax is reserved for blocks, so it's expecting the contents of a block - which it doesn't find. method { }
语法是为块保留的,因此期望块的内容-它找不到。 Rather, it finds a key-value association, which is reserved for hashes. 而是找到保留用于哈希的键值关联。 Essentially, Ruby tries to interpret it as a block, but fails.
本质上,Ruby尝试将其解释为一个块,但失败了。 So your options are to either encapsulate it with parentheses, like so:
method({ :hello => "world" })
, or drop the braces, like so: method :hello => "world"
. 因此,您的选择是用括号将其封装起来,例如:
method({ :hello => "world" })
,或者放下括号,例如: method :hello => "world"
。 You'll also need to add a do
at the end of your link_to
to have the content be a block, so your code looks like this now: 您还需要在
link_to
的末尾添加一个do
,以使内容成为一个块,因此您的代码现在如下所示:
li
= link_to :controller => "controllername", :action => "actionname" do
span.glyphicon.sidebar-icon.glyphicon-ok-circle
span WhoHas
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.