Ruby-on-rails SimpleForm 不带 for(非模型形式)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5181143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:34:57  来源:igfitidea点击:

SimpleForm without for (non model form)

ruby-on-railsruby-on-rails-3simple-form

提问by Edward Ford

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

是否可以在没有模型的情况下使用 Simple Form(作者:Plataformatec)?

https://github.com/plataformatec/simple_form

https://github.com/plataformatec/simple_form

回答by htanata

You can use :symbolas the first argument.

您可以:symbol用作第一个参数。

<%= simple_form_for :user, url: users_path do |f| %>
  <%= f.input :name, as: :string %>
  ...
<% end %>

It will output something like this:

它将输出如下内容:

<form novalidate="novalidate" class="simple_form user" action="/users" accept-charset="UTF-8" method="post">
  ...
  <div class="input string required user_name">
    <label class="string required" for="user_name">
      <abbr title="required">*</abbr> Name
    </label>
    <input class="string required" type="text" name="user[name]" id="user_name" />
  </div>
  ...
</form>

回答by Miguel Madero

Unfortunately simple_form relies on using a model. Essentially it would be nice to have something like simple_form_tag and input_tag methods equivalent to their rails *_tag helpers. Until then, there's an easy work around.

不幸的是 simple_form 依赖于使用模型。本质上,如果有类似 simple_form_tag 和 input_tag 方法等价于它们的 rails *_tag 助手会很好。在此之前,有一个简单的解决方法。

Use a symbol instead of the class in the form and pass the value explicitly to prevent simple_form from trying to access the model properties.

在表单中使用符号而不是类并显式传递值以防止 simple_form 尝试访问模型属性。

<%= simple_form_for :user, :url => '/users' do |f| %>
  <%= f.text_field :name, input_html: { value: nil } %>
<% end %>

This will avoid the undefined method 'name' for Usererror.

这将避免undefined method 'name' for User错误。

回答by jacr1614

You can also use fields outside the model within a form model, with simple_fields_for like this:

您还可以在表单模型中使用模型外部的字段,像这样 simple_fields_for:

<%= simple_form_for @user do |f| %>
  <%= f.input :name %>

  <%= simple_fields_for :no_model_fields do |n| %>
    <%= n.input :other_field %>
  <% end %>
<% end %>

This is simple and practical solution, because you can create different kind of fields from different models or without using models

这是一个简单实用的解决方案,因为您可以从不同的模型或不使用模型创建不同类型的字段

回答by FreePender

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.

要模仿 simple_form 的样式/优点,但删除对象/符号依赖项和强制数据嵌套,您可以创建部分。

HAMLexamples:

HAML例子:

form view:

表单视图:

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

fieldpartial:

field部分的:

- 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}" }

回答by Trung Lê

You could also pass a :symbolinstead of @objectas argument for simple_form_for.

您也可以传递一个:symbol而不是@object作为simple_form_for.

<%= simple_form_for :email, :url => '/post_email' do |f| %>
  <%= f.input :subject, :as => :string %>
<% end %>

Which would output:

这将输出:

<form method="post" class="simple_form email" action="/post_email" accept-charset="UTF-8">
  ...
  <input type="text" size="30" name="email[subject]" id="email_subject">
</form>

Please be aware of following draw-backs:

请注意以下缺点:

  • You won't be able to take advantage of automatic model validation
  • Need to explicitly define :urland the type of each input
  • 您将无法利用自动模型验证
  • 需要明确定义:url每个的类型input