Rails Simpleform with non-model inputs

前端 未结 3 1204
北海茫月
北海茫月 2020-12-15 20:35

I have a normal form using simpleform. Now I\'d like to add an input that does not have any corresponding field in the model, it will be processed by the controller. I tried

3条回答
  •  温柔的废话
    2020-12-15 21:17

    Yes, below are quote from simple_form wiki

    String Input

    app/inputs/fake_input.rb:

    class FakeInput < SimpleForm::Inputs::StringInput
      # This method only create a basic input without reading any value from object
      def input(wrapper_options = nil)
        merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
        template.text_field_tag(attribute_name, nil, merged_input_options)
      end
    end
    

    Then you can do <%= f.input :thing, as: :fake %>

提交回复
热议问题