ruby on rails - Automatically add hidden field to form -


i trying build custom form builder in simple_form, add hidden fields form without using form.hidden_field. noticed utf8 , authenticity_token hidden fields automatically added every form.

is there similar mechanism add custom hidden field, but forms generated custom form builder?

instead of patching @ formbuilder level, can integrate custom input:

class magicinput < simpleform::inputs::hiddeninput   def input     if object.condition?       @builder.hidden_field(:hidden_field_name, value: "some value").html_safe       # call #super here (instead of previous call)       # because extends hiddeninput (might cleaner, depending on       # want achieve)     end   end end 

this custom input injects hidden field form if object.condition? true. you'll need create #condition? method on object passed form (or replace condition line wathever floats boat).

then in view you'd call like:

= f.input :something, as: :magic 

and hidden field appear when object.condition? passes.

edit: , juicy detials - utf8 , authenticity_token hidden fields implemented in form_tag - not in formbuilder: https://github.com/rails/rails/blob/801e159ce2f4645f6839c94ab0febf97a0d8543d/actionview/lib/action_view/helpers/form_tag_helper.rb#l713


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -