Skip to content

Conversation

Copy link

Copilot AI commented Sep 19, 2025

Problem

When creating users through the admin interface (/admin/users) with invalid form data, the application was returning a 500 Internal Server Error instead of displaying helpful validation error messages. This occurred due to a NoMethodError in the form error handling code.

The specific error was:

NoMethodError (undefined method '+' for nil):
config/initializers/action_view_fields_with_errors.rb:8:in 'block in <main>'

This happened when form fields didn't have an existing class attribute, causing element["class"] to return nil, and the code attempted to concatenate a string to nil using the + operator.

Solution

Fixed the field_error_proc in config/initializers/action_view_fields_with_errors.rb to safely handle nil class attributes:

Before (buggy code):

if element && %w[input].include?(element.name)
  element["class"] += " is-invalid"  # ❌ NoMethodError when class is nil
  fragment.to_html.html_safe

After (fixed code):

if element && %w[input].include?(element.name)
  existing_class = element["class"] || ""
  element["class"] = existing_class.empty? ? "is-invalid" : "#{existing_class} is-invalid"
  fragment.to_html.html_safe

Testing

Added comprehensive tests to ensure the fix works correctly:

  1. Unit tests for the field error proc covering:

    • Form fields with no class attribute
    • Form fields with empty class attribute
    • Form fields with existing class attributes
    • Non-input elements (should remain unchanged)
  2. Integration tests for admin user creation scenarios:

    • Valid user creation (should succeed)
    • Invalid password length (should show validation errors, not 500)
    • Invalid email format (should show validation errors, not 500)
    • Duplicate email addresses (should show validation errors, not 500)

Impact

  • ✅ Admin users now see proper validation error messages instead of 500 errors
  • ✅ Form fields are properly highlighted with is-invalid CSS class on validation errors
  • ✅ Better user experience for admin user management
  • ✅ No breaking changes to existing functionality

This fix resolves the core issue where validation failures in the admin interface caused server crashes instead of displaying user-friendly error messages.

Fixes #3700.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Author

Copilot AI commented Sep 19, 2025

@pglombardo 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI changed the title [WIP] Admin User Creation Returns 500 Error Instead of Validation Messages Fix NoMethodError in admin user form validation - handle nil class attribute Sep 19, 2025
Copilot AI requested a review from pglombardo September 19, 2025 13:41
Copilot finished work on behalf of pglombardo September 19, 2025 13:41
@pglombardo
Copy link
Owner

Admin interface is getting replaced in v1.63.0 so this PR is unnecessary.

@pglombardo pglombardo closed this Oct 26, 2025
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.

Admin User Creation Returns 500 Error Instead of Validation Messages

2 participants