[英]Rails+Slim: How to use link_to tag with controller & action name; and nested content in the anchor tag
这就是我现在所拥有的:
li
= link_to {:controller => "controllername", :action => "actionname"}
span.glyphicon.sidebar-icon.glyphicon-ok-circle
span WhoHas
但是,这将无法正常工作,因为link_to
期望它的第一个参数是anchor标记内的内容。 我想要的锚标记内的内容是以下span
块。
任何帮助将非常感激!
你这样说:
但是,这将无法正常工作,因为link_to期望它的第一个参数是anchor标记内的内容。
这不是真的。 link_to
也可以接受一个块,它将使用该块的内容:
link_to("/path/to/thing") do
"My link"
end
并且,因此,不需要将第一个参数用作链接的文本。 该文档列出了以下签名:
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
最后两个选项是您要寻找的。
但是,代码不起作用的真正原因是因为您使用的括号不带括号-基本上, method { :hello => "world" }
并没有达到您的预期。 method { }
语法是为块保留的,因此期望块的内容-它找不到。 而是找到保留用于哈希的键值关联。 本质上,Ruby尝试将其解释为一个块,但失败了。 因此,您的选择是用括号将其封装起来,例如: method({ :hello => "world" })
,或者放下括号,例如: method :hello => "world"
。 您还需要在link_to
的末尾添加一个do
,以使内容成为一个块,因此您的代码现在如下所示:
li
= link_to :controller => "controllername", :action => "actionname" do
span.glyphicon.sidebar-icon.glyphicon-ok-circle
span WhoHas
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.