简体   繁体   English

帮助从一个HTML表单到另一个HTML表单的数据传输/存储

[英]Help with data transfer/storage from one HTML form to another

In my application, I have two forms for creating a user. 在我的应用程序中,我有两种用于创建用户的表单。
Form 1: I input the following details from the user in form 1. 表格1:我从表格1的用户输入以下详细信息。

  • Full Name 全名
  • Email Address 电子邮件地址
  • Gender 性别
  • Date of Birth 出生日期
  • Profile Image 个人资料图片

AND in the Form 2, I ask him to fill in his 在表格2中,我请他填写他的

  • desired @username, and 想要@username,和
  • password 密码

Problem is that I have associated my models in the following fashion, that 问题是我以下列方式关联我的模型

User has_one UserProfile  
UserProfile belongs_to User  

&

Email Address, username and password are attributes of User model. 电子邮件地址,用户名和密码是用户模型的属性。
The rest of the attributes belong to UserProfile model. 其余属性属于UserProfile模型。

But in my forms, I am using Email Address in Form1 and the other attributes in Form2. 但在我的表单中,我使用Form1中的电子邮件地址和Form2中的其他属性。

Should I make the forms as Rails forms or plain HTML forms? 我应该将表单作为Rails表单或纯HTML表单吗?
How should I do this??? 我该怎么办?

UPDATE:- I don't want to create a UserProfile Object without creating a User object, so I need some way to store the form 1 values in javascript, and then when Form 2 gets submitted, then pass them both to Rails controller method. 更新: -我不想在不创建User对象的情况下创建UserProfile对象,因此我需要一些方法将表单1值存储在javascript中,然后在提交表单2时,将它们都传递给Rails控制器方法。

I dont know much about ruby but whats the need to complicate the process.. Have one form and visually you can separate the fields using maybe two fieldsets. 我不太了解ruby,但是需要使这个过程复杂化。有一个表单,在视觉上你可以使用两个字段集分隔字段。 Then submit the form and at the backend save them any way you want. 然后提交表单并在后端以任何方式保存它们。

I don't see the need to create both UserProfile and User but if you do have to create it, and you first have to fill in form1 which is UserProfile and only then form2 which is User then you could create the following. 我不认为需要同时创建UserProfile和User,但是如果你必须创建它,并且首先必须填写form1,即UserProfile,然后只填写form2即User,那么你可以创建以下内容。

On the form1 first create a User.new and use User.save, then you have the new User.id and supposingly a new UserProfile has been created with the :reference to user_id, 在form1上首先创建一个User.new并使用User.save,然后你有了新的User.id,并且假设已经使用:user_id的引用创建了一个新的UserProfile,

(be aware that your validation process is more difficult because now if you validate :username it is null and therefore not unique nor whatever, you could make User.new defaults and call the data 'tempUser' until it is fully created.) (请注意,您的验证过程更加困难,因为现在如果您验证:username是null,因此不是唯一的,也不是什么,您可以使User.new默认并调用数据'tempUser',直到它完全创建。)

then you have the user_id to reference to, fill in the UserProfile, save it, redirect back to the User form, update it, validate the user entered data, and save it. 然后你有user_id来引用,填写UserProfile,保存它,重定向回用户表单,更新它,验证用户输入的数据,并保存它。

Why would you like to roll like that though? 你为什么要像那样滚?

Wouldn't having just User and retrieving the data? 不会只有用户和检索数据?

Or less validation in the process would be first having the user create the User object (username, password first), only then create the UserProfile object (email and such). 或者,在该过程中较少的验证将首先让用户创建User对象(用户名,密码优先),然后才创建UserProfile对象(电子邮件等)。

There are ways to bypass all of that, it's just more complicated :( 有办法绕过所有这一切,它只是更复杂:(

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

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