← Back to Upcase

Proper Route for Update


(Nicolo) #1

I have a challenge object in my database and one of the actions is to accept a challenge. A challenge has a state and when a challenge is accepted it does a variety of things including creating a game and setting the challenge state to accepted. I’m debating between two approaches, but feel free to suggest a 3rd.

  1. Update action on challenge controller

    class ChallengesController < ApplicationController
    def update
    do accept challenge action
    end
    end

2 Update action on accept_challenge controller

class AcceptChallengesController < ApplicationController
  def update
     Do accept challenge action
  end
end

(Nicolo) #2

I’m also realizing that I’ll need to send additional data as a parameter that isn’t a property of challenge. Not sure if that changes the approach.

I will also need to update state for cancelling challenges. In this case additional data would not need to be passed. If I used challenge update route it would be responsible for updating and cancelling challenges.


(Chad Pytel) #3

Hi @Nicolo,

I wouldn’t definitely go with the separate controller. I’d likely call it AcceptancesController, and I’d use the create action instead of update.

I’d also introduce an Acceptance class, which takes the parameters and contains the business logic for accepting a challenge - this class be be cleanly unit tested. You may even consider making this class an ActiveRecord object (Challenge has_many :acceptancesor Challenge has_on :acceptance?), if you want to save the acceptances and the additional parameters they had.

For canceling a challenge, I’d likely take the same approach - a CancellationsController, the create action, and a Cancellation class that contains the logic for canceling a challenge, which can be nicely unit tested. Again, you may consider making this class an ActiveRecord class, or not, it depends.

I hope that helps, let me know if I can clarify anything,
-Chad