-
Notifications
You must be signed in to change notification settings - Fork 24
Description
With the namespaces you can easily support a different name, but not so much the case with a Form. In the Form initialize method it uses the key method that looks up the param_key of the model by default, but there is no way to override this.
A use case for this is single table inheritance, when I want to create a form that supports either type, this is how the code already was.
In Rails you can easily override the namespace, and most FormBuilders seem to. I think this would make a great addition here as well,
for now it can only be solvedt by overriding the key method of my custom form.
# Rails
form_for(model, as: :node)
form_with(model:, scope: :node)Here you can see how scope precedes the model param_key lookup.
https://github.com/rails/rails/blob/1d1790b04d3930d65c2436bbe1d381af821d0ef4/actionview/lib/action_view/helpers/form_helper.rb#L771
I make the suggestion to add a paramter to give control over this from external scope, since its becomes the key of the root namespace and is a core concept i chose to keep it :namespace. But I could imagine others would prefer :root, or maybe looking for familiarity with rails :scope.
Superform::Rails::Form.new(model, namespace: :node)class Node < ApplicationRecord; end
class Leaf < Node; end
class NodeController < ApplicationController
def node_params
params.require('node').permit(...)
end
endIn this case one model param_key will give 'node' and the other 'leaf', but in the controller i want to depend on 'node' being the static namespace when dealing with parameters.
Related code of current Form implementation:
@namespace = Namespace.root(key, object: model, field_class: self.class::Field)
def key
@model.model_name.param_key
end