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?
, but NoMethodError at/card/new undefined method `exists? 'for nil: NilClass
What can I do to solve it?
Related questions
- ruby - passing variables using render partial
- ruby - #i want to see a list of posts for that user on the user details page
- ruby - implementation of friendly forwarding
- ruby - i installed carrierwave with rails, but i can't see the image
- ruby - about rails controller new action
- ruby - rails page does not open
- [ruby] i'm having trouble with nomethoderror
- ruby - [search function] acquisition of products associated with users
- ruby - even if each statement is used, the contents of the db cannot be displayed in order and an error occurs
- ruby - if i include the'omniauth-facebook'gem, do i need to install the'omniauth' gem as well?
As the error message says, there is no
I thinknil.exists?
method.find_by
returnsnil
if it doesn't hit after searching.if card.blank?
etc. should be used. (blank?
can be eithernil
or an empty relation.true
)