[英]Rails issue with the association for a form with nested attributes
I have the following models in my application: 我的应用程序中具有以下模型:
User: 用户:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
before_destroy { roles.clear }
has_many :users_roles
has_many :roles, through: :users_roles
accepts_nested_attributes_for :roles
#accepts_nested_attributes_for :user_extras
#id :confirmable is activated
def confirmation_required?
false
end
end
Role: 角色:
class Role < ApplicationRecord
#before_destroy { users.clear }
has_many :users, through: :users_roles
end
and UserRoles: 和UserRoles:
class UsersRoles < ApplicationRecord
belongs_to :user
belongs_to :role
end
I also have a form that will accept nested attributes, which looks something like this: 我也有一个可以接受嵌套属性的表单,看起来像这样:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<%= f.fields_for :roles do |field| %>
<%= field.label :name %></div>
<%= field.text_field :name %>
<% end %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
Also I have a controller that renders this form: 我也有一个呈现此表单的控制器:
class Admin::AccountController < ApplicationController
before_action :authenticate_user!
layout "admin/layouts/dashboard"
def index
@resource = User.new
@resource2 = Role.new
@resource.roles << @resource2
p "==================="
p @resource
p @resource2
p "==================="
@resource.roles.build
# respond_to do |format|
# format.html
# format.json { render json: @resource }
# end
end
private
def admin_account_params
params.require(:resource).permit(:email, :password, :password_confirmation, roles_attributes: [ :name ])
end
end
The thing is when I visit the page, I get the following error: 问题是当我访问页面时,出现以下错误:
uninitialized constant User::UsersRole
Extracted source (around line #9):
7
8
9
10
11
12
@resource = User.new
@resource2 = Role.new
@resource.roles << @resource2
p "==================="
I am not sure what is wrong with this at the moment. 我现在不确定这有什么问题。 It will be great if you can spot the problem. 如果能够发现问题,那将是很好的。 Many thanks in advance. 提前谢谢了。
Model classes by convention should be singular so the model class name should be... 按照惯例,模型类应为单数,因此模型类名称应为...
class UsersRole < ApplicationRecord
belongs_to :user
belongs_to :role
end
This will automatically map to a database table named users_roles
这将自动映射到名为users_roles
的数据库表
If you insist on using a plural class name then you need to specify the class name explicitly 如果您坚持使用复数类名,则需要显式指定类名
has_many :users_roles, class_name: 'UsersRoles'
And you'll need this in both the User
model and the Role
model 而且在User
模型和Role
模型中都需要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.