基本上我想按帐户(account_id)来调整资源范围,所以我在我的帐户base_controller中创建了一个名为current_account的方法和帮助器。
我最初使用子域名作为唯一标识符,但现在我想使用子域名删除,并让每个用户(包括我的帐户所有者用户)通过account_id关联。
我遇到的问题是我无法在帐户控制器中找出正确的方法来构建帐户并将account_id归属于所有者用户。我认为这可能与我已经在同一时间建立所有者的方式有关。帐户所有者很重要,因为他们有权向帐户添加/邀请新用户。
有人可以帮忙吗?
背景
帐户模型
class Account < ActiveRecord::Base
belongs_to :owner, class_name: "User"
accepts_nested_attributes_for :owner
validates :subdomain, presence: true, uniqueness: true
has_many :users
has_many :contacts
has_many :invitations
end
用户模型
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :account
end
帐户控制器
def new
@account = Account.new
@account.build_owner
end
def create
@user = current_user
@account = @user.account.build_user(account_params)
if @account.save
sign_in(@account.owner)
flash[:notice] = "Your account has been created."
redirect_to dashboard_index_path(current_user)
# redirect_to root_url(subdomain: @account.subdomain)
else
flash.now[:alert] = "Sorry, your account could not be created."
render :new
end
end
模式
create_table "accounts", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "owner_id"
t.string "subdomain"
end
add_index "accounts", ["subdomain"], name: "index_accounts_on_subdomain"
create_table "invitations", force: true do |t|
t.string "email"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "token"
end
add_index "invitations", ["account_id"], name: "index_invitations_on_account_id"
add_index "invitations", ["token"], name: "index_invitations_on_token"
create_table "users", force: true 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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "account_id"
end
add_index "users", ["account_id"], name: "index_users_on_account_id"
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
答案 0 :(得分:0)
我有一个类似的应用程序。代替组织帐户。这是我的模型/控制器......
class Organization < ApplicationRecord
has_many :users
has_many :tasks, through: :users
end
class User < ApplicationRecord
belongs_to :organization
has_many :tasks, dependent: :destroy
accepts_nested_attributes_for :organization
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def self.get_users(current_org)
current_org.users
end
end
不是在控制器中创建组织(帐户),而是在用户注册时执行此操作。当新用户注册时,他们输入组织名称。是views / devise / registrations / new view
<h2>Sign up</h2>
<% resource.organization ||= Organization.new %>
<%= 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>
<%= f.fields_for :organization do |org| %>
<div><%= 'Organization or Company Name' %><br />
<%= org.text_field :name %></div>
<% end %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
注册的第一个用户被设置为组织的管理员用户,并且可以访问用户仪表板,允许在组织内创建其他用户。
以下是该应用程序的工作示例以及根文件夹中的readme.pdf:
https://github.com/marklocklear/devise_multitenant_rails5
希望这有帮助!