link_to_除非这样做

发布于 2025-01-05 01:16:41 字数 324 浏览 4 评论 0原文

这是一个典型的 link_to_unless

<%= link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) %>

我怎样才能用块做同样的事情?

# non-functional
<%= link_to_unless(@current_user.nil?, ..., { :action => "reply" }) do %>
  ...
<% end %>

Here's a typical link_to_unless:

<%= link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) %>

How can I do the same thing with a block?

# non-functional
<%= link_to_unless(@current_user.nil?, ..., { :action => "reply" }) do %>
  ...
<% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牛↙奶布丁 2025-01-12 01:16:41

link_to_unless 已接受一个块。不过,您需要传递一个块参数来访问链接标题参数。

link_to_unless(@current_user.nil?, "Name", { :action => "reply" }) do |name|
  link_to name, some_path

此时,您必须问自己是否更容易执行 if/else 语句。

link_to_unless already accepts a block. You need to pass a block argument for to access the link title parameter, though.

link_to_unless(@current_user.nil?, "Name", { :action => "reply" }) do |name|
  link_to name, some_path

At this point though you have to ask yourself if it's just easier to do and if/else statement.

半窗疏影 2025-01-12 01:16:41

把它放在助手中

def link_to_unless_with_block condition, uri, &block
   link_to_unless condition, capture(&block), uri
end 

应该可以工作..虽然没有经过测试

put this in a helper

def link_to_unless_with_block condition, uri, &block
   link_to_unless condition, capture(&block), uri
end 

should work.. though not tested

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文