SimpleForm without for (non model form)

前端 未结 5 1927
情书的邮戳
情书的邮戳 2020-11-30 21:57

Is it possible to use Simple Form (by: Plataformatec) without a model?

https://github.com/plataformatec/simple_form

5条回答
  •  渐次进展
    2020-11-30 23:04

    All of the methods above still leave you with form data nested inside of "user" or whatever symbol that you pass as the first argument. That's annoying.

    To mimic simple_form's style/benefits, but remove the object/symbol dependency and the forced data nesting, you can create a partial.

    HAML examples:

    form view:

    = form_tag("path/to/action", method: "POST") do
        = render "path/to/partial/field", type: "string", required: true, item: "first_name"
    

    field partial:

    - required_string = required ? "required" : ""
    %div{class: "input #{type} #{required_string} #{item}"}
      %label{class: "#{type} #{required_string}", for: "#{item}"}
        - if required
          %abbr{title: "required"}
            *
        = t("application.#{item}")
      %input{name: "#{item}",                                                     |
        placeholder: t("application.#{item}"),                                    |
        type: "#{type}",                                                          |
        required: required,                                                       |
        "aria-required" => "#{required}" }
    

提交回复
热议问题