Search code examples
haskellihp

redirectTo works but does not change URL in IHP


Context

I have 2 controllers:

  • LessonsController (with views Index and Show)
  • QuizAnswersController (no views)

In accordance with the generated controller code, in LessonsController the ShowLessonAction renders ShowView.

In ShowView, I have a link to an action in QuizAnswersController:

<a href={CheckQuizAnswerAction (get #id answer)}>Check Answer</a>

CheckQuizAnswerAction checks if the answer is correct and then redirects to the ShowLessonAction with a success message:

action CheckQuizAnswerAction { quizAnswerId } = do
    quizAnswer <- fetch quizAnswerId
    quiz <- fetch (get #quizId quizAnswer)
    let lessonId = get #lessonId quiz

    case (get #correct quizAnswer) of
        True -> setSuccessMessage "Correct!" >> redirectTo ShowLessonAction { lessonId }
        False -> setSuccessMessage "Try again" >> redirectTo ShowLessonAction { lessonId  }

Question

Everything works, but I noticed that after the CheckQuizAnswerAction is called, the URL is:

/CheckQuizAnswer?quizAnswerId=123...

instead of:

/ShowLesson?lessonId=123...

What is going on to make this work with a different URL?


Solution

  • As your action is doing a side effect it's best to use a form here:

    <form method="POST" action={CheckQuizAnswerAction (get #id answer)}>
        <button type="submit" class="btn btn-link">Check Answer</button>
    </form>
    

    Using this instead of the <a href={CheckQuizAnswerAction (get #id answer)}>Check Answer</a> might already fix the issue :)