带有Rails的同一页面上的多个表单

时间:2022-11-06 10:04:17

So I'm building a rails app for high school students and I've hit a problem when it comes to creating users.

所以我正在为高中生构建一个rails应用程序,在创建用户时遇到了问题。

I want the students to only be able to create accounts if they select their school and type in their school's password correctly.

我希望学生只有在选择学校并正确输入学校密码后才能创建帐户。

What is the correct / easiest way of doing this? Should I create a gatekeeper to the user#new action that they have to pass first or if their a way that on the same page a student can submit to forms. One would be the regular username, email, password using:

这样做的最正确/最简单的方法是什么?我应该为用户创建一个看门人#他们必须首先传递的新动作,或者他们是否可以在同一页面上为学生提交表单。一个是常规用户名,电子邮件,密码使用:

form_for @user do
  ...
end

But then creating another form for the high-school / high-school password selection.

但随后为高中/高中密码选择创建了另一种形式。

Ideally the controller would be able to get the params of the high-school form, validate those, then go on to create the user from the user params.

理想情况下,控制器将能够获得高中形式的参数,验证这些参数,然后继续从用户参数创建用户。

Is this possible using rails?

这可能使用导轨吗?

My setup: Rails 3 and Ruby 1.9.2dev

我的设置:Rails 3和Ruby 1.9.2dev

Thank you!

3 个解决方案

#1


9  

You can do this with fields_for (http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for)

您可以使用fields_for(http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for)执行此操作

Your view will look something like

你的观点看起来像

form_for @user do |f|
  fields_for @school do |school|
    school.text_field     :name
    school.password_field :password
  end

  f.text_field     :username
  f.text_field     :email
  f.password_field :password
  f.submit "Submit"
end

Then in your controller, params[:user] will represent the user data, and params[:school] will represent the school name/password.

然后在你的控制器中,params [:user]将代表用户数据,params [:school]将代表学校名称/密码。

You can then validate the correct school password before creating the user.

然后,您可以在创建用户之前验证正确的学校密码。

#2


4  

jrallison is right about fields_for, but I would suggest considering whether it's strictly necessary to do both on the same form.

jrallison对于fields_for是正确的,但我建议考虑是否必须在同一表格上进行两者。

What you're describing with the school name and password sounds a lot like user authentication. It might not be a "user" in the sense of a single person, but in this case the "user" is the school. Maybe you should treat it that way: have a separate login form, create a session if they succeed (so that they won't have to type their school's password over and over again later on), and do the standard flash messages and such if they fail.

您用学校名称和密码描述的内容听起来很像用户身份验证。它可能不是一个人的“用户”,但在这种情况下,“用户”是学校。也许你应该这样对待它:拥有一个单独的登录表单,如果成功就创建一个会话(以便他们以后不必一次又一次地输入他们学校的密码),并执行标准的flash消息等等。他们失败了。

At that point, the form you need is simple and single-model. And if other features become necessary later on that also need the school password, you're already a step ahead.

那时,您需要的形式是简单的单一模型。如果以后需要其他功能也需要学校密码,那么您已经领先一步了。

#3


0  

I'm not familiar with Rails 3, but this'll certainly work with Rails 2. Some adaptation might be necessary.

我不熟悉Rails 3,但这肯定适用于Rails 2.有些适应可能是必要的。

Providing your User model would help, but I'll assume that it's something like: a User belongs_to :school, and a School has a field named password. In your User model you can have something like:

提供您的用户模型会有所帮助,但我会假设它类似于:用户belongs_to:school,而School有一个名为password的字段。在您的用户模型中,您可以使用以下内容:

validates_associated :school
attr_accessible :school_password

def validate_on_create
  unless self.school_password == school.password
    errors.add :school_password, "incorrect password, please try again"
  end
end

Then you can have a select box in the form, like

然后你可以在表单中有一个选择框,比如

form_for @user do |user|
  user.select :school...
  user.password_field :school_password
end

#1


9  

You can do this with fields_for (http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for)

您可以使用fields_for(http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for)执行此操作

Your view will look something like

你的观点看起来像

form_for @user do |f|
  fields_for @school do |school|
    school.text_field     :name
    school.password_field :password
  end

  f.text_field     :username
  f.text_field     :email
  f.password_field :password
  f.submit "Submit"
end

Then in your controller, params[:user] will represent the user data, and params[:school] will represent the school name/password.

然后在你的控制器中,params [:user]将代表用户数据,params [:school]将代表学校名称/密码。

You can then validate the correct school password before creating the user.

然后,您可以在创建用户之前验证正确的学校密码。

#2


4  

jrallison is right about fields_for, but I would suggest considering whether it's strictly necessary to do both on the same form.

jrallison对于fields_for是正确的,但我建议考虑是否必须在同一表格上进行两者。

What you're describing with the school name and password sounds a lot like user authentication. It might not be a "user" in the sense of a single person, but in this case the "user" is the school. Maybe you should treat it that way: have a separate login form, create a session if they succeed (so that they won't have to type their school's password over and over again later on), and do the standard flash messages and such if they fail.

您用学校名称和密码描述的内容听起来很像用户身份验证。它可能不是一个人的“用户”,但在这种情况下,“用户”是学校。也许你应该这样对待它:拥有一个单独的登录表单,如果成功就创建一个会话(以便他们以后不必一次又一次地输入他们学校的密码),并执行标准的flash消息等等。他们失败了。

At that point, the form you need is simple and single-model. And if other features become necessary later on that also need the school password, you're already a step ahead.

那时,您需要的形式是简单的单一模型。如果以后需要其他功能也需要学校密码,那么您已经领先一步了。

#3


0  

I'm not familiar with Rails 3, but this'll certainly work with Rails 2. Some adaptation might be necessary.

我不熟悉Rails 3,但这肯定适用于Rails 2.有些适应可能是必要的。

Providing your User model would help, but I'll assume that it's something like: a User belongs_to :school, and a School has a field named password. In your User model you can have something like:

提供您的用户模型会有所帮助,但我会假设它类似于:用户belongs_to:school,而School有一个名为password的字段。在您的用户模型中,您可以使用以下内容:

validates_associated :school
attr_accessible :school_password

def validate_on_create
  unless self.school_password == school.password
    errors.add :school_password, "incorrect password, please try again"
  end
end

Then you can have a select box in the form, like

然后你可以在表单中有一个选择框,比如

form_for @user do |user|
  user.select :school...
  user.password_field :school_password
end