Ruby-on-rails Rails 3 / 没有模型的表单:如何创建与模型无关的表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4805774/
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:11:54  来源:igfitidea点击:

Rails 3 / Form without Model: How do I create a form that is not tied to a model?

ruby-on-railsruby-on-rails-3activerecord

提问by Morris Singer

I have a model, and I have a view that displays a form for creating a new object based on that model. Let's call that form, Form1.

我有一个模型,我有一个视图,该视图显示用于基于该模型创建新对象的表单。我们称这种形式为Form1

Once the user submits Form1, the object is created. I then want to display, on the following page, a second form Form2, which asks the user to check off various options before the object is saved to the database.

一旦用户提交Form1,对象就被创建了。然后我想在下一页显示第二个表单Form2,它要求用户在将对象保存到数据库之前检查各种选项。

My problem is probably extremely basic. I don't know how to create Form2, given that it is not tied directly to the model. Because I am a Railsnewbie, I have only created forms as following:

我的问题可能非常基本。我不知道如何创建Form2,因为它没有直接绑定到模型。因为我是Rails新手,所以我只创建了如下表格:

form_for(@object) { |f| ... }

@objectis an object instantiated from the model

@object是从模型实例化的对象

Problem: I do not believe that kind of code will work for my purposes here. How do I create Form2, given that it must not be based on @objector @object's model?

问题:我不相信这种代码可以满足我的目的。Form2鉴于它不能基于@objector@object的模型,我该如何创建?

Some specifics from my app:

我的应用程序的一些细节

The site accepts values (Form1) before redirecting to an OAuthserver.

该站点Form1在重定向到OAuth服务器之前接受值 ( ) 。

When the user verifies her credentials on the OAuthserver, she is redirected back to my site. An XML-RPCrequest then retrieves information about the user's account on the OAuthserver.

当用户在OAuth服务器上验证她的凭据时,她将被重定向回我的站点。一个XML-RPC请求,然后获取关于用户的账户上的信息OAuth服务器。

The XMLresponse may indicate that the user has only one account on the OAuthserver. If so, some values are retrieved from the XMLand added to the object--which is then (finally) saved in the database--and the user is redirected to a success page.

XML响应可能表明用户在OAuth服务器上只有一个帐户。如果是,则从 中检索某些值XML并将其添加到对象中——然后(最终)保存在数据库中——并且用户被重定向到成功页面。

However, if the XMLresponse indicates that the user has multiple accounts on the OAuthserver, I want to display a form (Form2) that allows the user to select which accounts on the OAuth server to associate with my site. So Form2really asks the user how manyobjects to create, rather than about specific attributes of an object.

但是,如果XML响应表明用户在OAuth服务器上有多个帐户,我想显示一个表单 ( Form2),允许用户选择 OAuth 服务器上的哪些帐户与我的站点相关联。所以Form2真正询问用户要创建多少个对象,而不是询问对象的特定属性。

回答by Dylan Markow

Use form_taginstead of form_for, then use the appropriate form helpers: text_field_taginstead of f.text_field, text_area_taginstead of f.text_area, etc. Example:

使用form_tag代替form_for,然后使用适当的表单助手:text_field_tag代替f.text_fieldtext_area_tag代替f.text_area等。示例:

<%= form_tag "/my_controller/update2" do %>
  <%= text_field_tag "account", "default info" %>
  <%= submit_tag "Save" %>
<% end %>

The Rails API site has a great reference to all of the _tag helpers: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

Rails API 站点对所有 _tag 助手有很好的参考:http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

回答by Karl Rosaen

Starting in rails3, validations have been decoupled from ActiveRecord so you can create vanilla objects that can be used as validators with the form helpers:

从 rails3 开始,验证已与 ActiveRecord 分离,因此您可以使用表单助手创建可用作验证器的普通对象:

 class Person
   include ActiveModel::Validations

   attr_accessor :first_name, :last_name

   validates_each :first_name, :last_name do |record, attr, value|
     record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
   end
 end

and then in your template:

然后在您的模板中:

<%= form_for(Person.new) do |f| %>
...

It's a handy way to get the benefits of the form helpers for things like email forms without having to create a model object tied to your schema.

这是一种方便的方法,可以在无需创建与您的架构相关联的模型对象的情况下,为电子邮件表单之类的事物获得表单助手的好处。

回答by Lahiru

To create a table-less model,

要创建无表模型,

class Person
  include ActiveModel::Model

  attr_accessor :first_name, :last_name

  validates :first_name, :last_name, presence: true
end

Then in your view,

那么在你看来,

<%= form_for(Person.new) do |f| %>
 .... your form ....
<% end %>

Another similar solution can be found at RailsCasts: Active-Model

可以在以下位置找到另一个类似的解决方案RailsCastsActive-Model

回答by Eric Woodruff

If you are looking to use SimpleForm then there is a specific answer for that here Does form_tag work with Simple_form?

如果您想使用 SimpleForm 那么这里有一个具体的答案form_tag 与 Simple_form 一起工作吗?