Bitwise
Bitwise

Reputation: 8451

Send "nil" in params but display different value

I have a scenario where the client wants the value of Null displayed in the view for a particular field if it is nil. But, if that value is nil I don't actually want to send the value if Null to the server I still want to treat that field as if it were nil and nothing was sent. Here is what I'm working with:

Field:

<%= form.text_field :fee, value: finfolio_fee_presence(@account.fee), class: "finfolio-fields__input", readonly: true %>

Helper:

def finfolio_fee_presence(value)
  value.nil? ? "Null" : value
end

So, this displays the value to the user that the client wants but I don't actually want to send "Null" in this case. How can I treat this as nil and not send this field to the server if it is nil?

Upvotes: 1

Views: 86

Answers (2)

Mohit
Mohit

Reputation: 11

<%= form.text_field :fee, 
  value: finfolio_fee_presence(@account.fee), 
  class: 'finfolio-fields__input',
  disabled: @account.fee.nil? %>

Upvotes: 0

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33450

What about using the disabled option in case the @account.fee is nil?, it'll show your text_field and its "tweaked" value, but it won't go within the params:

<%= form.text_field :fee, 
      value: finfolio_fee_presence(@account.fee), 
      class: 'finfolio-fields__input',
      disabled: @account.fee.nil? %>

Upvotes: 1

Related Questions