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 Rails
newbie, I have only created forms as following:
我的问题可能是非常基本的。我不知道如何创建Form2,因为它没有直接绑定到模型。因为我是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. An XML-RPC
request then retrieves information about the user's account on the OAuth
server.
当用户在OAuth服务器上验证她的凭证时,她将被重定向回我的站点。然后,XML-RPC请求检索关于OAuth服务器上用户帐户的信息。
The XML
response may indicate that the user has only one account on the OAuth
server. 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响应可能表明用户在OAuth服务器上只有一个帐户。如果是这样,就从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. So Form2
really asks the user how many objects to create, rather than about specific attributes of an object.
但是,如果XML响应表明用户在OAuth服务器上有多个帐户,我希望显示一个表单(Form2),允许用户选择OAuth服务器上的哪些帐户与我的站点关联。Form2会问用户要创建多少对象,而不是对象的特定属性。
4 个解决方案
#1
115
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_field, text_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 helper都有很好的引用:http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
#2
35
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解耦了,这样您就可以创建可以作为表单helper的验证器的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.
这是一种方便的方式,可以获得表单助手的好处,比如电子邮件表单,而不必创建与模式绑定的模型对象。
#3
4
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吗?
#4
4
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
另一个类似的解决方案可以在railscast中找到:Active-Model
#1
115
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_field, text_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 helper都有很好的引用:http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
#2
35
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解耦了,这样您就可以创建可以作为表单helper的验证器的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.
这是一种方便的方式,可以获得表单助手的好处,比如电子邮件表单,而不必创建与模式绑定的模型对象。
#3
4
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吗?
#4
4
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
另一个类似的解决方案可以在railscast中找到:Active-Model