Controller action being re-run is different from expected.
westoj opened this issue · comments
Describe the bug
I'm not sure if this is by design or not, however:
When using stimulus reflex on a standard rails form and the object fails validation, stimulus reflex seems to re-run the controller action based on the GET equivalent of the URL of the form's POST request.
Essentially if you have a view with a form with stimulus reflex somewhere on that template and follow a typical rails pattern:
def new
@some_instance = Model.new
end
def create
@some_instance = Model.new(model_params)
if @some_instance.save
redirect_to models_path, notice: "Model created successfully"
else
render :new
end
end
and the object fails validation the next time any reflex action on that page is stimulated
the controller action that will be rerun is, in this example, models_path
i.e ModelsController#index
and not ModelsController#new
as you'd might logically expect.
To Reproduce
Any standard rails form with an object that fails validation with some SR elements on the page.
Expected behavior
The controller action to be rerun should be the same one that rendered the template in the first place, i.e the most recent GET
Versions
StimulusReflex
- Gem: [e.g. 3.4.1]
External tools
- Ruby: [e.g. 3.0.2]
- Rails: [e.g. 6.1.4.1]
Hi @westoj! SR will always run based on the current page URL.
The problem that you are describing is best addressed by using UJS remote forms, which have been part of Rails since 3.2, although in Rails 6.1 forms became local: true
by default in anticipation of Turbo.
Our preferred approach is to use the mrujs library, which will take the markup returned by a validation failure and morph it into the current page. Our opinion is that this is how UJS should have worked all along.