Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error handling for ajax http request #64

Open
Xiaohong-Deng opened this issue Sep 30, 2017 · 1 comment
Open

Better error handling for ajax http request #64

Xiaohong-Deng opened this issue Sep 30, 2017 · 1 comment

Comments

@Xiaohong-Deng
Copy link
Owner

Xiaohong-Deng commented Sep 30, 2017

now we have two controller actions for ajax calls which in successful cases don't serve a html page back, just a html snippet for inserting or a state indicating success.

now the problem is if something wrong happens, what we can do to inform the user something is wrong? redirect_to is not going to work. I haven't tried render and I doubt that would be any better.

one is Answers#update

  def update
    authorize @answer
    if @answer.make_judge_identified!
      ActionCable.server.broadcast "game-#{@answer.question.game_id}: info", { message: "Judge made his choice", message_type: "info" }
      head :ok
    else
      # send from a remote call, so no redirect_to actually
      flash[:alert] = "Judge choice has not been updated."
      redirect_to root_path
    end
  end

note that if authorize @answer fails the error gets handled by a general purpose method, which uses redirect_to

the other is Games#judge

  def judge
    authorize @game
    render layout: false
  end
@Xiaohong-Deng
Copy link
Owner Author

In the happy path, we did broadcast info to users and return a 200 ok to the browser. We can use a javascript callback to do something with the 200 ok (ajax:success)

In the sad path, we might be able to use the oppsite info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant