Ruby on rails Rails-设计-将配置文件信息添加到单独的表中
我正在使用Desive在我的应用程序中构建注册/身份验证系统 已经查看了许多用于向Desive模型添加信息的资源(例如用户名、传记、头像URL等)[资源包括 这一切都很好——它可以工作。但我的问题是它正在保存到用户模型,根据(),它实际上应该保存到用户的子模型,该子模型通过Ruby on rails Rails-设计-将配置文件信息添加到单独的表中,ruby-on-rails,ruby,devise,Ruby On Rails,Ruby,Devise,我正在使用Desive在我的应用程序中构建注册/身份验证系统 已经查看了许多用于向Desive模型添加信息的资源(例如用户名、传记、头像URL等)[资源包括 这一切都很好——它可以工作。但我的问题是它正在保存到用户模型,根据(),它实际上应该保存到用户的子模型,该子模型通过连接,它有一个并且属于 到目前为止,我已经通过designe创建了一个User模型。我还通过rails generate脚本创建了一个UserProfile模型 user.rb(供参考) class用户
连接,它有一个并且属于
到目前为止,我已经通过designe创建了一个User
模型。我还通过rails generate
脚本创建了一个UserProfile
模型
user.rb(供参考)
class用户
用户配置文件.rb
class UserProfile < ActiveRecord::Base
belongs_to :user
end
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
class-UserProfile
创建用户配置文件的时间戳。rb
class UserProfile < ActiveRecord::Base
belongs_to :user
end
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
class CreateUserProfiles
我现在的问题是,如何收集这两种模型的信息,并通过设计登记表确保所有信息最终都在正确的位置
我已经看到了关于创建状态机的参考资料(,和)。我还看到了关于创建状态机的信息,以及关于同一主题的信息
对于我的用例来说,这些似乎都太复杂了。有没有什么方法可以简单地用Desive分离输入,并确保最终结果正确无误?请访问RailsCasts.com网站
关于嵌套模型表单,有几个有趣的railscasts:
也请查看
或者看看这个问题:
我想,我不会简单地对一个答案进行评论从而得出最终答案,我会将答案存档在这里,以防将来有人试图找到这个答案:
我将假设你有某种设置,就像我上面所做的那样
第一步是您需要修改您的用户控制器,以便为
配置文件引用接受\u嵌套的\u属性\u,并向模型添加实用程序方法,以便在代码中请求时,应用程序可以检索构建的配置文件模型或构建一个
用户模型最终看起来是这样的:
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
在我的具体案例中,这方面的一个例子是:
<%= 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 %>
最后,您需要告诉Desive它应该接受这个新的散列参数并将其传递给模型
如果您已经创建了自己的RegistrationController和extended Desive,那么它应该类似于以下内容:
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:email, :password, user_profile_attributes: :username)
end
end
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
类注册控制器
(当然,针对您的特定用例进行适当的更改。)
如果您只是将Desive Sanitation方法添加到您的应用程序控制器中,则其外观应与此类似:
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:email, :password, user_profile_attributes: :username)
end
end
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
class ApplicationController
(同样,针对您的特定用例进行适当的更改。)
关于user\u profile\u attributes::username
的小说明:
注意,这当然是一个散列。如果您要传递多个属性,例如作为帐户\u更新
(提示提示),则需要像传递用户配置文件\u属性那样传递它们:[:属性\u 1,:属性\u 2,:属性\u 3]
还请注意,对于Desive 4.2,Desive_参数_消毒剂的“.for”方法被弃用,取而代之的是“.permit”
从:
你提到的文档和其他问题最终让我找到了答案。