Files
fizzy/app/controllers/commands_controller.rb
T
Jorge Manrubia 55ea460d3e Handle form errors with a stimulus class
We need to keep the focus in place when submitting the form. We are using a data-turbo-permanent for that. We were rendering the error form with a turbo stream. The problem was that turbo streams ignore permanent elements as per this https://github.com/hotwired/turbo/pull/688, so the system wasn't updating the input field as expected.

Handling this as the stimulus level will also help with managing additional error states (such as the one we need for confirmations).
2025-05-07 08:46:47 +02:00

34 lines
743 B
Ruby

class CommandsController < ApplicationController
def index
@commands = Current.user.commands.order(created_at: :desc).limit(20)
end
def create
command = parse_command(params[:command])
if command&.valid?
result = command.execute
case result
when Command::Result::Redirection
redirect_to result.url
else
redirect_back_or_to root_path
end
else
head :unprocessable_entity
end
end
private
def parse_command(string)
Command::Parser.new(parsing_context).parse(string).tap do |command|
Current.user.commands << command
end
end
def parsing_context
Command::Parser::Context.new(Current.user, url: request.referrer)
end
end