1772298f2d14152c13bbd3221e6ab6ec

I've got the method below which would seem like an easy refactoring job as they are both fairly similar kinds of logic, however, I'm having trouble figuring out a better way of doing it. I essentially just need to make sure a user can only get to either the edit action or new action based on whether or not they have a credit card on file, but never be able to access both. Any ideas?

def new
    if current_user.has_credit_card_on_file?
      redirect_to edit_user_billing_path, :notice => "We've detected that you already have a credit card on file, please edit the details below"
    end
  end
  
  def edit
    unless current_user.has_credit_card_on_file?
      redirect_to new_user_billing_path, :notice => "We've detected that you do not have a credit card on file, please add one below"
    end
  end

Refactorings

No refactoring yet !

434a9b92916191c04485db77a05a494e

rsludge

August 20, 2011, August 20, 2011 09:16, permalink

No rating. Login to rate!

I think, you can use one action and render view depending on credit card presence.

def some_action
  if current_user.has_credit_card_on_file?
    render :new
  else
    render :edit
  end
end
434a9b92916191c04485db77a05a494e

rsludge

August 20, 2011, August 20, 2011 09:17, permalink

No rating. Login to rate!

I think, you can use one action and render view depending on credit card presence.

def some_action
  if current_user.has_credit_card_on_file?
    render :edit
  else
    render :new
  end
end

Your refactoring





Format Copy from initial code

or Cancel