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

Preserve user input when there are validation errors #1122

Merged
merged 4 commits into from
Nov 21, 2024

Conversation

etiennebarrie
Copy link
Member

Fix #1116

This allows preserving the data entered in the fields when there's a validation error and we're rendering the maintenance_tasks/tasks/show template.

I tried using TaskController.render(:show) to be able to call the tasks/show controller action directly, but I had issues with the flash (the flash is not used with ActionController::Renderer because it creates a fake request). We might be able to make this work, but instead, I decided to simplify the template by only requiring one instance variable to be passed into it, and then I was able to build the TaskDataShow in a different manner for the call site in runs#create, and finally just render the template for the other action.

I did use flash.now because we're now rendering the template directly instead of after a redirect. I also added a redirect for GET tasks/:task_id/runs back to the task page.

task_name = params.fetch(:id)
@task = TaskDataShow.new(task_name)
@task.active_runs.load
set_refresh if @task.active_runs.any?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove set_refresh now, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still used in tasks/index.

test/models/maintenance_tasks/task_data_show_test.rb Outdated Show resolved Hide resolved
@etiennebarrie etiennebarrie force-pushed the preserve-user-input branch 2 times, most recently from be7f450 to 5633221 Compare November 21, 2024 14:56
Use a single instance variable to pass all the data that is needed for
the view to render.

This requires increasing the role of TaskDataShow to include preloading
the active runs, and ensuring the Task exists, which was previously done
in the controller action.
This preserves the user input while showing the validation errors.
Since a validation error results in the URL being shown in the address
bar, it's a good practice to support the URL for GET requests. We just
redirect back to the task page.
@etiennebarrie etiennebarrie merged commit 118e8f9 into main Nov 21, 2024
31 checks passed
@etiennebarrie etiennebarrie deleted the preserve-user-input branch November 21, 2024 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't lose form data when there is a validation error
3 participants