- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 Devise 在我的应用程序中构建注册/身份验证系统。
查看了相当多的资源以将信息添加到设计模型(例如用户名、传记、头像 URL 等)[资源包括 Jaco Pretorius' website , this (badly formed) SO question , and this SO question .
一切都很好,很好——它有效。但我的问题是它正在保存到用户模型,根据 database normalizations ( also referencing this SO question ),它实际上应该保存到通过 has_one
和 belongs_to
连接的 User 子模型。
到目前为止,我已经通过 Devise 创建了一个 User
模型。我还通过 rails generate
脚本创建了一个 UserProfile
模型。
user.rb(供引用)
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable
has_one :user_profile, dependent: :destroy
end
user_profile.rb
class UserProfile < ActiveRecord::Base
belongs_to :user
end
timestamp_create_user_profiles.rb
class CreateUserProfiles < ActiveRecord::Migration
def change
create_table :user_profiles do |t|
t.string :username, null: false
t.string :biography, default: ""
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
add_index :user_profiles, [:user_id, :username]
end
end
我现在的问题是,如何收集这两种型号的信息,并通过设计注册表确保所有信息都在正确的位置?
我看过有关创建状态机( AASM 和 the answer to this SO question 的资源。我还看过关于同一主题的有关创建 a wizard with WICKED 和 an article 的信息。
这些对于我的用例来说似乎太复杂了。有什么方法可以简单地将输入与设计分开并确保最终在正确的位置吗?
最佳答案
我认为,与其简单地评论导致我找到最终答案的答案,,我会把答案存档在这里,以防将来有人也试图找到这个答案:
我将假设您已经像我上面那样进行了某种设置。
第一步是您需要将用户 Controller 修改为 accept_nested_attributes_for
配置文件引用,并向模型添加一个实用方法,以便在代码中请求时,应用程序可以检索构建的配置文件模型或 build 一个。
用户模型最终看起来像这样:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable
has_one :user_profile, dependent: :destroy
accepts_nested_attributes_for :user_profile
def user_profile
super || build_user_profile
end
end
其次,您需要修改注册/account_update 表单,以便能够将此辅助模型的属性传递到 Controller 中,并最终能够为父模型构建配置文件。
您可以使用 f.fields_for
来做到这一点。
在您的表单中添加类似这样的内容:
<%= f.fields_for :user_profile do |user_profile_form| %>
<%= user_profile_form.text_field :attribute %>
<% end %>
在我的具体案例中,这方面的一个例子是:
<%= f.fields_for :user_profile do |user_profile_form| %>
<div class="form-group">
<%= user_profile_form.text_field :username, class: "form-control", placeholder: "Username" %>
</div>
<% end %>
最后,您需要告诉 Devise 它应该接受这个新的参数散列并将其传递给模型。
如果您已经创建了自己的 RegistrationsController 并扩展了 Devise,它应该看起来类似于:
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:email, :password, user_profile_attributes: :username)
end
end
(当然,请针对您的特定用例进行适当的更改。)
如果您只是将 Devise 清理方法添加到您的应用程序 Controller ,它应该看起来类似于:
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) {|u|
u.permit(:email, :password, user_profile_attributes: :username)}
end
end
(同样,针对您的特定用例进行适当的更改。)
关于 user_profile_attributes: :username
的小注释:请注意,这当然是一个散列。如果您要传递的属性不止一个,比如说,作为 account_update
(提示提示),您需要像这样传递它们 user_profile_attributes: [:attribute_1, :attribute_2, : attribute_3]
.
关于ruby-on-rails - Rails - Devise - 将配置文件信息添加到单独的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051782/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!