Rails+Slim:如何使用带有控制器和动作名称的 link_to 标签;以及锚标签中的嵌套内容
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
期望它的第一个参数是锚标记内的内容。我想要在锚标记内的内容是以下 span
块。
如有任何帮助,我们将不胜感激!
你是这样说的:
However this won't work as link_to expects it's first argument to be the content inside the anchor tag.
这不是真的。 link_to
也可以接受一个块代替,它会使用块的内容代替:
link_to("/path/to/thing") do
"My link"
end
因此,不需要将第一个参数用作 link 的文本。 The documentation 列出以下签名:
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
希望对您有所帮助。
这是我现在拥有的:
li
= link_to {:controller => "controllername", :action => "actionname"}
span.glyphicon.sidebar-icon.glyphicon-ok-circle
span WhoHas
然而,这不会起作用,因为 link_to
期望它的第一个参数是锚标记内的内容。我想要在锚标记内的内容是以下 span
块。
如有任何帮助,我们将不胜感激!
你是这样说的:
However this won't work as link_to expects it's first argument to be the content inside the anchor tag.
这不是真的。 link_to
也可以接受一个块代替,它会使用块的内容代替:
link_to("/path/to/thing") do
"My link"
end
因此,不需要将第一个参数用作 link 的文本。 The documentation 列出以下签名:
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
希望对您有所帮助。