fix(chats): prevent blank chat creation from raising ActiveRecord::RecordInvalid - #3183
fix(chats): prevent blank chat creation from raising ActiveRecord::RecordInvalid#3183sentry[bot] wants to merge 1 commit into
Conversation
Manage your Superagent protectionSuperagent has paused scans for this repository because this unlinked GitHub App installation has used all three included PR scans. You have 0 of 3 included PR scans remaining. Create a free account to continue protection, manage scan settings, review security history, and control which repositories are protected. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 86c69acb86
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
|
||
| def create | ||
| if chat_params[:content].blank? | ||
| return redirect_to new_chat_path, alert: t(".content_blank") |
There was a problem hiding this comment.
Surface blank-message errors for Turbo frame submissions
When a blank message is submitted through the normal form, the form is inside the sidebar_chat Turbo frame (app/views/chats/new.html.erb), so this redirect only replaces that frame. The notification tray is outside the frame in the application layout, meaning Turbo discards the rendered alert and the flash is consumed by the redirected request; the user simply sees the blank form reset with no explanation. Return a Turbo Stream that updates the notification tray or render an error inside the chat frame.
Useful? React with 👍 / 👎.
| rescue ActiveRecord::RecordInvalid => e | ||
| redirect_to new_chat_path, alert: e.record.errors.full_messages.to_sentence |
There was a problem hiding this comment.
Limit the validation rescue to chat creation
If start! succeeds but set_last_viewed_chat raises ActiveRecord::RecordInvalid—for example, an existing user has a locale that is no longer in I18n.available_locales—this method has already committed the chat and initial message and triggered its after-commit response job. The broad rescue then redirects to the new-chat form as though creation failed, so retrying creates a duplicate chat and may issue another provider request. Restrict the rescue to start! or wrap creation and the user update in one transaction.
Useful? React with 👍 / 👎.
This PR addresses the
ActiveRecord::RecordInvaliderror that occurs when a user attempts to create a chat with blank content.Previously,
ChatsController#createwould directly callChat.start!with the provided content. If the content was blank,Chat.start!would attempt to create aChatrecord with a blank title (derived from the blank content) and aUserMessagewith blank content. Both of these actions violateChatandMessagemodel validations, leading to an unhandledActiveRecord::RecordInvaliderror.This fix implements two layers of protection in
ChatsController#create:createaction to immediately redirect the user back to the new chat form with an alert ifchat_params[:content]is blank. This prevents invalid data from reaching the model layer.rescue ActiveRecord::RecordInvalidblock is added around theChat.start!call. This acts as a fallback, catching anyActiveRecord::RecordInvalidexceptions that might still occur (e.g., from other unexpected validations) and redirecting the user with a user-friendly error message.Additionally, the
en.ymllocale file has been updated to include the translation for the blank content alert message.Fixes SURE-APP-GC
This PR was automatically generated by Sentry. You can adjust this setting at any time.