In Rails, there are several times when I would like to add an attribute to something only if the attribute has a specific value.
Let me give you an example. Let's say I want to disable a button based on a specific property, for example check_if_user_can_be_added:
link_to 'Create account', new_user_path, disabled: (user_can_be_added?)
It all looks good and good, except that disabled can be applied to HTML no matter what value you give it. If you give the button an attribute disabled: false, it will still be disabled.
What I need
link_to 'Create account', new_user_path, disabled: true
link_to 'Create account', new_user_path
The way I can do it
Getting this means that you need a solution like the following, which first sets up a hash of parameters, and then passes it afterwards:
options = user_can_be_added? ? {disabled: true} : {}
link_to 'Create account', new_user_path, options
The way I would like to do this ...
, Ruby. , - . ,
link_to 'Create account', new_user_path, ({disabled: true} if user_can_be_added?)
, , - splat, ??