How to separate methods
card = Card.find_by (user_id: current_user.id) is also used in other actions, so I was told to divide it into methods, but I don't know how to do it.
Please professor

class PurchaseController  10000,
    : customer =>card.customer_id,
    : currency =>'jpy',
  redirect_to action: 'done'
  def done
    card = current_user.cards.first
    redirect_to controller: "card", action: "new" if card.blank?
  • Answer # 1

    I wonder if it is necessary to cut out such a short, but if you dare do it
    before_action: set_card, only: Write [: index,: pay]at the beginning

    def set_card
      @card = Card.find_by (user_id: current_user.id)