rails 6 设计创建用户和帐户 ActiveModel::UnknownAttributeError(用户的未知属性 'accounts'。):
rails 6 Devise create user & account ActiveModel::UnknownAttributeError (unknown attribute 'accounts' for User.):
您好,我有一个 rails 6 应用程序,我希望用户在注册时创建一个帐户。
我正在使用设备进行身份验证。
我有两个模型 User(devise) 和 account
class User < ApplicationRecord
has_merit
enum role: [:user, :tech, :admin, :manager]
belongs_to :account
accepts_nested_attributes_for :account
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :admin
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :invitable, :registerable,
:recoverable, :rememberable, :validatable
end
class Account < ApplicationRecord
has_many :users, dependent: :destroy
has_many :clients, dependent: :destroy
end
在注册(设计注册)期间,我希望用户创建一个帐户。
在控制台中运行:
User.create(email: 'test@email.com', password: "test", password_confirmation: "test, account_attributes: {name: "testaccount"})
按预期创建。
但是当我使用前端表单时
<h2>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email,
required: true,
autofocus: true,
input_html: { autocomplete: "email" }%>
<%= f.simple_fields_for :accounts do |a| %>
<%= a.input :name %>
<% end %>
<%= f.input :password,
required: true,
hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
input_html: { autocomplete: "new-password" } %>
<%= f.input :password_confirmation,
required: true,
input_html: { autocomplete: "new-password" } %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
我收到一个奇怪的错误
ActiveModel::UnknownAttributeError (unknown attribute 'accounts' for User.):
我的架构
create_table "accounts", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.integer "role"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "sash_id"
t.integer "level", default: 0
t.bigint "account_id"
t.index ["account_id"], name: "index_users_on_account_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
非常感谢您的帮助,如果您需要更多信息,请告诉我。
您得到 UnknownAttributeError
是因为您的 users
table 中没有名为 accounts
.
的列
我建议你这样做:
#user.rb
has_one :account, inverse_of: :user
accepts_nested_attributes_for :account
#account.rb
belong_to :user
#controller
def new
@user = User.new
@user.build_account
end
来源:https://github.com/plataformatec/simple_form/wiki/Nested-Models#nested-models
您好,我有一个 rails 6 应用程序,我希望用户在注册时创建一个帐户。
我正在使用设备进行身份验证。
我有两个模型 User(devise) 和 account
class User < ApplicationRecord
has_merit
enum role: [:user, :tech, :admin, :manager]
belongs_to :account
accepts_nested_attributes_for :account
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :admin
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :invitable, :registerable,
:recoverable, :rememberable, :validatable
end
class Account < ApplicationRecord
has_many :users, dependent: :destroy
has_many :clients, dependent: :destroy
end
在注册(设计注册)期间,我希望用户创建一个帐户。
在控制台中运行:
User.create(email: 'test@email.com', password: "test", password_confirmation: "test, account_attributes: {name: "testaccount"})
按预期创建。
但是当我使用前端表单时
<h2>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email,
required: true,
autofocus: true,
input_html: { autocomplete: "email" }%>
<%= f.simple_fields_for :accounts do |a| %>
<%= a.input :name %>
<% end %>
<%= f.input :password,
required: true,
hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
input_html: { autocomplete: "new-password" } %>
<%= f.input :password_confirmation,
required: true,
input_html: { autocomplete: "new-password" } %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
我收到一个奇怪的错误
ActiveModel::UnknownAttributeError (unknown attribute 'accounts' for User.):
我的架构
create_table "accounts", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.integer "role"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "sash_id"
t.integer "level", default: 0
t.bigint "account_id"
t.index ["account_id"], name: "index_users_on_account_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
非常感谢您的帮助,如果您需要更多信息,请告诉我。
您得到 UnknownAttributeError
是因为您的 users
table 中没有名为 accounts
.
我建议你这样做:
#user.rb
has_one :account, inverse_of: :user
accepts_nested_attributes_for :account
#account.rb
belong_to :user
#controller
def new
@user = User.new
@user.build_account
end
来源:https://github.com/plataformatec/simple_form/wiki/Nested-Models#nested-models