0
I持って自分のアプリケーションでは、次のモデル:Railsの問題
ユーザー:
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
役割:
class Role < ApplicationRecord
#before_destroy { users.clear }
has_many :users, through: :users_roles
end
とUserRoles:
class UsersRoles < ApplicationRecord
belongs_to :user
belongs_to :role
end
私もこのフォームをレンダリングするコントローラは、Iをしている。また
<%= 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 %>
:
は私がページを訪問したときに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
事は、私が取得され、次のようなものに見えるネストされた属性を受け入れるメートル、エラー:
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 "==================="
現時点で何が間違っているかわかりません。あなたが問題を見つけることができればそれは素晴らしいことでしょう。事前に多くの感謝。慣例により
説明ありがとうございます! –