0

I have a form which is shared among some controllers. Sometimes I pass in isbn argument, sometimes not.

= form_for book do |f|
    - if isbn.present?
      = f.hidden_field :virtual_isbn_id, :value => isbn.id

How do I check if isbn is populated or not? Alternatively, how can I set isbn to be nil by default?

Thank you.

AdamNYC
  • 19,887
  • 29
  • 98
  • 154

2 Answers2

2

Try defined? isbn to check if the variable exists.

- if defined? isbn and isbn
  = "isbn exists and is not nil"
Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57
0

Use if defined? isbn .... You should take a look at this Question for example usage: Checking if a variable is defined?

Alternatively you can use isbn.blank? or isbn.nil? to check if the value is nil or blank (e.g. an empty string) if you know that the variable is present.

Community
  • 1
  • 1
Simon Woker
  • 4,994
  • 1
  • 27
  • 41