如何使用Ruby on Rails在注册流程中添加用户配置文件表单字段?

2026-04-28 02:151阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计498个文字,预计阅读时间需要2分钟。

如何使用Ruby on Rails在注册流程中添加用户配置文件表单字段?

我有一个rails 3.1应用,使用devise。用户模型通过has_one关联一个个人资料,个人资料通过belongs_to关联用户。我已转发devise的registration_controller来自定义注册视图,一切正常。现在我想补充以下内容:在注册页面上,我想添加一个表单字段。

我有一个rails 3.1 app with devise:

>用户has_one个人资料
>个人资料belongs_to用户
>推翻了devise registration_controller
>自定义注册视图一切正常,注册工作正常

如何使用Ruby on Rails在注册流程中添加用户配置文件表单字段?

现在我想补充一下:

>在注册页面上,我想添加个人资料中的字段,如名字,姓氏
>还没有用户,将在提交表单时创建
>我需要使用此名字创建配置文件,最后一个

我该怎么做?我尝试了几个想法,也来自堆栈溢出,但似乎无法让它工作.我尝试嵌套属性,这是不行的方法,这样做会在用户注册时在数据库中创建一个配置文件记录,插入名字和姓氏

我的注册#new view:

= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| = devise_error_messages! = f.input :username, :label => 'Username' = f.input :email, :label => 'Email' = f.input :password, :label => 'Password' = f.input :password_confirmation, :label => 'Password confirm' // start fields for profile = f.fields_for :profile do |f| = f.label :bod_day = f.text_field :bod_day // end fields for profile = f.button :submit, t(:submit, :scope => :register)

在我的用户模型中,我有这个:

accepts_nested_attributes_for :profile 我认为问题是在呈现表单时该用户上不存在任何配置文件,一种简单的方法可以在渲染字段之前在内存中构建它:

= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| = devise_error_messages! = f.input :username, :label => 'Username' = f.input :email, :label => 'Email' = f.input :password, :label => 'Password' = f.input :password_confirmation, :label => 'Password confirm' // make a new profile in memory = resource.build_profile // start fields for profile = f.fields_for :profile do |f| = f.label :bod_day = f.text_field :bod_day // end fields for profile = f.button :submit, t(:submit, :scope => :register)

本文共计498个文字,预计阅读时间需要2分钟。

如何使用Ruby on Rails在注册流程中添加用户配置文件表单字段?

我有一个rails 3.1应用,使用devise。用户模型通过has_one关联一个个人资料,个人资料通过belongs_to关联用户。我已转发devise的registration_controller来自定义注册视图,一切正常。现在我想补充以下内容:在注册页面上,我想添加一个表单字段。

我有一个rails 3.1 app with devise:

>用户has_one个人资料
>个人资料belongs_to用户
>推翻了devise registration_controller
>自定义注册视图一切正常,注册工作正常

如何使用Ruby on Rails在注册流程中添加用户配置文件表单字段?

现在我想补充一下:

>在注册页面上,我想添加个人资料中的字段,如名字,姓氏
>还没有用户,将在提交表单时创建
>我需要使用此名字创建配置文件,最后一个

我该怎么做?我尝试了几个想法,也来自堆栈溢出,但似乎无法让它工作.我尝试嵌套属性,这是不行的方法,这样做会在用户注册时在数据库中创建一个配置文件记录,插入名字和姓氏

我的注册#new view:

= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| = devise_error_messages! = f.input :username, :label => 'Username' = f.input :email, :label => 'Email' = f.input :password, :label => 'Password' = f.input :password_confirmation, :label => 'Password confirm' // start fields for profile = f.fields_for :profile do |f| = f.label :bod_day = f.text_field :bod_day // end fields for profile = f.button :submit, t(:submit, :scope => :register)

在我的用户模型中,我有这个:

accepts_nested_attributes_for :profile 我认为问题是在呈现表单时该用户上不存在任何配置文件,一种简单的方法可以在渲染字段之前在内存中构建它:

= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| = devise_error_messages! = f.input :username, :label => 'Username' = f.input :email, :label => 'Email' = f.input :password, :label => 'Password' = f.input :password_confirmation, :label => 'Password confirm' // make a new profile in memory = resource.build_profile // start fields for profile = f.fields_for :profile do |f| = f.label :bod_day = f.text_field :bod_day // end fields for profile = f.button :submit, t(:submit, :scope => :register)