Home>

card = Card.where (user_id: current_user.id) .first
redirect_to action: "show" if card.exists?

Use this with find_by

card = Card.find_by (user_id: current_user.id)
redirect_to action: "show" if card.exists?

I rewrote it to

, but NoMethodError at/card/new undefined method `exists? 'for nil: NilClass
What can I do to solve it?

  • Answer # 1

      

    NoMethodError at/card/new undefined method `exists? 'for nil: NilClass

    As the error message says, there is nonil.exists?method.find_byreturnsnilif it doesn't hit after searching.

    I think

    if card.blank?etc. should be used. (blank?can be eithernilor an empty relation.true)