Rails 表单和关联 - 使用数量表单添加多个对象
我是 Rails 新手,但对编程或数据库不熟悉。
我的问题的更好措辞在下面的回答中。
为了简单起见,在这个例子中,我有 3 个模型: 用户 订阅 Default_Subscription
User has_many Subscriptions
Subscription belongs_to User
Default_Subscription has_many Subscriptions
Subscription belongs_to Default_Subscription
Default_Subscription 是一个预先填充的表,其中包含某些类型的订阅。
在订阅过程中,默认订阅会在某一时刻列出,并且有 每个旁边都有一个数量框。
用户输入每个订阅的数量并点击“提交”以继续。
我的问题是: 如何以数字形式为每个数量创建一个新的订阅?
因此,您将得到一个如下所示的列表:
<ol>
<%= each subscription with quantity box %>
</ol>
<%= button_to %>
当用户点击按钮时,如何将每个框中的数量相加并添加每人一个新的订阅?我必须使用 javascript 来获取数字吗?即使这个数量与任何特定的数据库字段没有关联,我是否以某种方式使用 Rails 表单?任何建议或指出我自己解决这个问题的正确方向都会很棒。
此表单框不是任何模型的字段,它是关联的计数。让我重新表述一下:表单框中的每个数量代表要创建的新订阅的数量。这些订阅中的每一个都属于 1 个 DEFAULT_SUBSCRIPTION。本质上,该数字表示与该默认订阅相关的新订阅的数量。
我正在使用 Rails 3.2.1 和 ruby 1.8.7
谢谢
I am new to rails, but not to programming or databases.
A BETTER PHRASING OF MY QUESTION IS IN MY ANSWER BELOW.
For simplicity in this example, I have 3 models:
User
Subscription
Default_Subscription
User has_many Subscriptions
Subscription belongs_to User
Default_Subscription has_many Subscriptions
Subscription belongs_to Default_Subscription
Default_Subscription is a pre-populated table with certain types of subscriptions.
During the subscription process, the default subscriptions are listed at one point, and there
is a quantity box alongside each one.
The user enters the quantities for each subscription and hits submit to continue on.
My question is:
How would one go about creating a new subscription for each quantity in a number form?
So you would have a list something like so:
<ol>
<%= each subscription with quantity box %>
</ol>
<%= button_to %>
When the user hits the button, how do you add up the quantity from each box and add a new subscription for each one? Do I have to use javascript to get the numbers? Do I somehow use rails forms even though this quantities are not associated with any specific database field? Any recommendations or pointing me in the right direction to figure this out on my own would be great.
This form box IS NOT A FIELD FOR ANY MODEL, it's a count for an association. Let me rephrase: Each quantity in the form boxes represent the number of NEW Subscriptions to be created. Each of these subscriptions BELONGS_TO 1 DEFAULT_SUBSCRIPTION. In essence, the number represents the number of new subscriptions ASSOCIATED WITH THAT DEFAULT SUBSCRIPTION.
I'm using rails 3.2.1, and ruby 1.8.7
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经找到了一种方法,并再次阅读我原来的帖子,整件事真的很令人困惑,因为我不知道如何准确地说出我想要完成的事情。我必须说,我感到困惑的很多原因是因为我想要的表单不对应于任何模型,只是一个关联计数,如果你想创建一堆没有属性的新对象,这最终是一个基本的 html 表单然而。我将首先澄清然后展示我的解决方案。
说明:
我有 2 个表:
用户正在订阅我的网站。这个过程是一步一步进行的:并非所有事情都发生在同一页面上。
这一切都发生在 subscribe_controller 中。每个操作对应于流程中的一个步骤。
其中一项操作是default_subscriptions。此操作列出了用户可以选择的 Default_Subscription,但他们不仅可以选择,还可以为他们想要的每种类型的 Default_Subscription 输入金额。
当 Default_Subscriptions 列在 default_subscriptions 页面上时,我想要一个表单,其中每个 Default_Subscription 旁边都有一个 html 编号输入。当通过下一步按钮提交表单时,我不知道如何从每个 html 输入中收集数量并创建一个 Subscription.new 数组,其中每个订阅的 default_subscription_id 对应于正确的 Default_Subscription。
一个可能的解决方案:
假设在default_subscriptions页面上输入所有数量后我想要进入的页面是review_subscriptions。
以下是我创建正确表单以继续控制器中的下一个操作所做的操作:
这里的技巧是将字符串传递给 number_field_tag。通过在 field_tag 方法参数的字符串末尾放置一组方括号,括号之前的部分是哈希的名称,括号中的内容是哈希中的键,提交按钮会导致每个键对应的值就是字段的值。很酷!
传递给下一个操作的参数将包含一个名为 subscription_counts 的哈希值,迭代此哈希值将为每个 default_subscription_id 提供相应的新订阅金额。像这样:
我只想指出,与他们合作越多,我就越爱他们;我喜欢 Rails,也喜欢 Ruby。他们超级有趣而且优雅。直到循环...这有多酷?如果您有其他解决方案,既然我的问题更加明显,请加入!我知道其他人正在尝试找到一些巧妙的方法来通过像这样的单个 post 调用以一对多关联创建多个新对象。从技术上讲,我的对象尚未保存在数据库中,但这现在并不难。
对我实现此解决方案最有帮助的主要参考是:
http://guides.rubyonrails.org/form_helpers.html
如果您是 Rails 新手,并且对表单感到困惑,请阅读本文。我现在感觉自己像个大师了。 Rails 开发人员非常擅长记录事物!
I have figured out one way, and reading my original post again, the whole thing is really confusing because I didn't know how to say exactly what I was trying to accomplish. I must say a lot of the reason I was confused is because the form I wanted did not correspond to any model, just an association count, which is ultimately a basic html form if you want to create a bunch of new objects without having their attributes yet. I'll first clarify then show my solution.
Clarification:
I have 2 Tables:
A User is subscribing to my website. This process is a step by step: not everything happens on the same page.
This all happens in a subscribe_controller. Each action corresponds to a step in the process.
One of the actions is default_subscriptions. This action lists the Default_Subscriptions a User can choose from, except they do not just choose, they can enter an amount for each type of Default_Subscription they'd like.
When the Default_Subscriptions are listed on the default_subscriptions page, I wanted a form with an html number input alongside each of these Default_Subscription. When the form is submitted via a next button, I had no idea how to gather the quantities from each html input and create an array of Subscription.new, with each Subscription's default_subscription_id corresponding to the proper Default_Subscription.
One Possible Solution:
Lets say the page I want proceed to after all the quantities are entered on the default_subscriptions page is review_subscriptions.
Here's what I did to create the proper form to proceed to the next action in the controller:
The trick here is that string passed to the number_field_tag. By placing a single set of square brackets at the end of the string for a field_tag method parameter, the part before the brackets is the name of the hash, and the thing in the brackets is a key in the hash, and the submit button causes the corresponding value for each key to be the value of the field. Pretty cool!
The parameters passed to the next action would contain a hash called subscription_counts, and iterating through this hash would give a corresponding new subscription amount for each default_subscription_id. Like so:
I'd just like to point out, the more I work with them, the more I love them; I love Rails, and I love Ruby. They are super fun and classy. An until loop... how cool is that? If you have other solutions, now that my question is more obvious, please chime in! I know others out there are trying to find some slick ways to create multiple new objects in a one to many association with a single post call like this. Technically my objects aren't saved in the database yet, but that wouldn't be to hard now.
The main reference which helped me the most in reaching this solution was:
http://guides.rubyonrails.org/form_helpers.html
If you are new to rails, and confused about forms, read this. I feel like a master now. Rails devs are really good at documenting things!
不确定我完全理解你的描述,但我会尝试一下。
使用 'form_for' 函数基于 @default_subscription 的实例(在控制器中的“new”操作中建立)构建表单。如果@default_subscription中有默认值,将显示在字段中,用户可以根据需要更改它们。 (假设您的 DefaultSubscription 模型具有三个属性:sub1、sub2 和 sub3。)
当用户单击提交按钮时,我们将表单的内容组装成哈希并通过 params[] 传递到控制器的“更新”操作中。您可以像这样提取订阅哈希:
此时,您已拥有用户在 user_subscription 哈希字段中输入的所有数字。您现在可以解析哈希以提取数字,进行数学计算,然后根据用户的输入创建适当的订阅。 (注意:数字可以是字符串,您可能需要将它们转换回整数,如下所示。)
例如,要合计所有订阅值并将该总数保存到用户的订阅中:
正如我所说,我不清楚您的描述,所以这可能没有密切关系,但希望它接近您正在寻找的内容。
祝你好运。
Not sure I totally understand your description, but I'll take a shot.
Use the 'form_for' function to build your form, based on an instance of @default_subscription (established in the "new" action in your controller). If there are default values in @default_subscription, will show in the fields and the user can change them as they see fit. (this assumes your DefaultSubscription model has three attributes: sub1, sub2 and sub3.)
When the user clicks the submit button the contents of the form with we assembled into a hash and passed into your controller's "update" action via params[]. You can extract the subscription hash like this:
At this point you have all the numbers that the user entered into the fields in the user_subscription hash. You can now parse the hash to extract the numbers, do your math, and then create the appropriate subscriptions per the user's input. (one note: the numbers could be strings and you might need to convert them back to integers as I've shown below.)
For example, to total all the subscription values and save that total into a user's subscription:
As I said, I don't understand your description clearly, so this might not be germane, but hope it is close to what you were looking for.
Good luck.