'Turbo stream in Rails 7 does not render the same pages for error action of create

My controller is as follows:

  def create
    @message = @inbox.messages.new(message_params) 

    respond_to do |format|
      if @message.save
        format.turbo_stream do
          render turbo_stream: [
            turbo_stream.update('new_message',
                                 partial: 'inboxes/messages/form',
                                 locals: { message: Message.new })
          ]
        end
        format.html { redirect_to @inbox, notice: "Message was successfully created." }
      else

        format.turbo_stream do
          render turbo_stream: turbo_stream.update('new_message', partial: 'inboxes/messages/form', locals: { message: @message })
        end
        format.html { render :new, status: :unprocessable_entity }

      end
    end
  end

The create action redirect to @inbox without issue but when I try to render the error (else) it's redirected to inboxes/messages/

Also dont know why but ActionController::UnknownFormat with the following code only for the else part:

def create
    @message = @inbox.messages.new(message_params)

    respond_to do |format|
      if @message.save
        format.turbo_stream do
          render turbo_stream: [
            
            turbo_stream.update('new_message',
                                partial: 'inboxes/messages/form',
                                locals: { message: Message.new })
 
            ]
        end
        format.html { redirect_to @inbox, notice: 'Message was successfully created.' }

      else
        format.turbo_stream do
          render turbo_stream: [
            turbo_stream.update('new_message',
                                partial: 'inboxes/messages/form',
                                locals: { message: @message })
            ]
          format.html { render :new, status: :unprocessable_entity }
        end
      end
    end
  end



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source