【问题标题】:Is there an inline way to conditionally add an attribute in Ruby?是否有内联方式有条件地在 Ruby 中添加属性?
【发布时间】:2014-05-23 15:06:20
【问题描述】:

在 Rails 中有很多次我希望能够将属性添加到某些东西只有当属性具有特定值时

让我举个例子。假设我想禁用基于特定属性的按钮,例如check_if_user_can_be_added

link_to 'Create account', new_user_path, disabled: (user_can_be_added?)

这一切看起来都很好,除了 disabled 碰巧在 HTML 中应用,不管你给它什么值。如果你给一个按钮属性disabled: false,那么它仍然会被禁用。

我需要什么

# if the button is disabled
link_to 'Create account', new_user_path, disabled: true

# if the button is not disabled
link_to 'Create account', new_user_path

我能想到的方法

得到这个意味着您需要一个类似于以下的解决方案,它首先设置选项哈希,然后将其传递:

options = user_can_be_added? ? {disabled: true} : {}
link_to 'Create account', new_user_path, options

我想这样做的方式......

这不起作用,但相信 Ruby 的美丽,我怀疑那里有类似的东西。这基本上就是我想做的事情

link_to 'Create account', new_user_path, ({disabled: true} if user_can_be_added?)

我能做到吗,是否有什么东西可以使用 splat 运算符让我到达那里...?

【问题讨论】:

  • Rails 在使用这样的助手时应该考虑布尔属性——使用 disabled: false 应该可以工作,完全忽略 disabled 属性。

标签: html ruby-on-rails ruby


【解决方案1】:

你可以设置 nil 来让 Rails 忽略这个属性:

link_to 'Create account', new_user_path, disabled: (user_can_be_added? ? true : nil)

对于这种特殊情况,您还可以使用 ||像这样:

link_to 'Create account', new_user_path, disabled: (user_can_be_added? || nil)

【讨论】:

  • 可爱 - 很有效。我不知道 nil 导致 Rails 跳过参数。正是我所需要的,OR 语句也是一种优雅的风格。 TY
【解决方案2】:

我不熟悉rails,但是在ruby中可以实现如下内联if语句:

(condition ? true : false)

我假设您的代码如下所示:

link_to 'Create account', new_user_path, (user_can_be_added? ? disabled: true : nil)

如果 user_can_be_added? 解析为 false,这实际上会传入 nil 来代替 disable: true

不过,我不确定link_to 函数会如何处理这个问题。我猜disabled: false 不起作用的原因是link_to 函数接收属性散列作为最终参数,这些参数应用于html 链接。由于 html 中的 disabled 属性不需要值,因此无论其值如何,它都会坚持使用 <a href="" disabled>。有人随时纠正我,我没有使用过 Rails。

【讨论】:

  • 不幸的是它不起作用。该方法需要一个键值对,因此如果您传递 nil 它只会引发错误。
  • 您是否尝试过使用占位符值?例如(user_can_be_added? ? disabled: true : tmp: "nothing")。虽然超级 hacky,但可能有更好的方法。
  • 这几乎就在那里,但需要调整。 @TomDavis 表明跳过参数的方法不是将键/值对作为 nil 传递,而是传递 key: nil 组合。
【解决方案3】:

几年前,我使用了一个 Rails 插件来处理 HTML 属性,该插件在 Github 上仍然可用。该插件允许编写这样的html标签:

content_tag(:div, :class => { :first => true, :second => false, :third => true }) { ... }
# => <div class="first third">...

当前版本的 Rails 不支持插件,插件仅适用于标签助手,但也许这有助于您自己编写助手。

【讨论】:

  • 你知道有没有办法通过视图助手直接添加 HTML 而不需要插件?
【解决方案4】:

对于许多参数,使用 splat 将起作用:

p "Here's 42 or nothing:", *(42 if rand > 0.42)

这不适用于哈希参数,因为它会将其转换为数组。

我不推荐它,但你可以使用 to_h (Ruby 2.0+):

link_to 'Create account', new_user_path, ({disabled: true} if user_can_be_added?).to_h

【讨论】:

    【解决方案5】:

    对我来说,这看起来是一个使用助手的好地方

    module AccountHelper 
    
      def link_to_create_account disabled = false
        if disabled
          link_to 'Create account', new_user_path, disabled: true
        else
          link_to 'Create account', new_user_path
        end
      end 
    end
    

    在您的 ERB 中,它只是 link_to_create_account(user_can_be_added?)

    不是每个人都喜欢帮手,但他们为我工作。

    【讨论】:

      猜你喜欢
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 2021-01-21
      • 2019-04-13
      • 2018-11-09
      • 1970-01-01
      • 2013-08-14
      相关资源
      最近更新 更多