简体   繁体   English

Rails 3 / Form没有Model:如何创建一个与模型无关的表单?

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

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. 用户提交Form1 ,将创建该对象。 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. 然后,我想在下一页上显示第二个表单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. 我不知道如何创建Form2 ,因为它不直接与模型绑定。 Because I am a Rails newbie, I have only created forms as following: 因为我是Rails新手,所以我只创建了以下表单:

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

@object is 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 @object or @object 's model? 如何创建Form2 ,因为它不能基于@object@object的模型?

Some specifics from my app : 我的应用程序中的一些细节

The site accepts values ( Form1 ) before redirecting to an OAuth server. 在重定向到OAuth服务器之前,站点接受值( Form1 )。

When the user verifies her credentials on the OAuth server, she is redirected back to my site. 当用户在OAuth服务器上验证其凭据时,会将其重定向回我的站点。 An XML-RPC request then retrieves information about the user's account on the OAuth server. 然后, XML-RPC请求将检索有关OAuth服务器上用户帐户的信息。

The XML response may indicate that the user has only one account on the OAuth server. XML响应可能表示用户在OAuth服务器上只有一个帐户。 If so, some values are retrieved from the XML and added to the object--which is then (finally) saved in the database--and the user is redirected to a success page. 如果是这样,则从XML检索一些值并将其添加到对象中 - 然后(最终)将其保存在数据库中 - 并将用户重定向到成功页面。

However, if the XML response indicates that the user has multiple accounts on the OAuth server, I want to display a form ( Form2 ) that allows the user to select which accounts on the OAuth server to associate with my site. 但是,如果XML响应表明用户在OAuth服务器上有多个帐户,我想显示一个表单( Form2 ),允许用户选择OAuth服务器上与我的站点关联的帐户。 So Form2 really asks the user how many objects to create, rather than about specific attributes of an object. 因此, Form2确实要求用户创建多少对象,而不是关于对象的特定属性。

Use form_tag instead of form_for , then use the appropriate form helpers: text_field_tag instead of f.text_field , text_area_tag instead 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

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分离,因此您可以创建可用作表单助手的验证器的vanilla对象:

 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. 这是一种方便的方法,可以在不必创建与模式绑定的模型对象的情况下,为电子邮件表单等内容提供表单帮助程序的好处。

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中找到

如果您正在寻找使用SimpleForm,那么这里有一个特定的答案吗?form_tag是否适用于Simple_form?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM