我正在使用 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/