【问题标题】:Pass Account id (parent) to User (child) with Devise in Rails使用 Rails 中的 Devise 将帐户 ID(父级)传递给用户(子级)
【发布时间】:2014-05-27 22:23:37
【问题描述】:

我已经设置了必要的模型和视图来拥有一个设计资源、帐户和用户。帐户 has_many users & User belongs_to account。架构将 account_id 反映为用户的属性。我可能弄错了,但我的印象是,当帐户登录并创建新用户时,此 account_id 属性会自动填充。检查 Rails 控制台后,似乎所有以这种方式创建的新用户的 account_id 值为 nil。单独的问题,但这是在使用 Devise 的应用程序中实现多租户的理想方式吗?

型号:

用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  belongs_to :account, :inverse_of => :users   
  accepts_nested_attributes_for :account
end

account.rb

class Account < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :users, :inverse_of => :account, :dependent => :destroy  
  accepts_nested_attributes_for :users
  has_many :projects

end

schema.rb(仅用户和帐户)

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"
    t.boolean  "supervisor"
    t.string   "name"
  end
create_table "accounts", 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.string   "name"
  end

users_controller.rb

class UsersController < ApplicationController
    before_filter :authenticate_user! 
    def new     
        @user = User.new   
    end    

    def create         
      @user.skip_confirmation! # confirm immediately--don't require email confirmation     
      if @user.save       
        flash[:success] = "User added and activated."       
        redirect_to users_path # list of all users     
      else       
        render 'new'     
      end   
    end

    def index
        @users = User.all
    end

end

accounts_controller.rb

class AccountsController < ApplicationController
  def new     
    @accounts = Account.new     
    @accounts.users.build  
  end    
  def create     
    @account = Account.new(params[:account])     
    if @account.save       
      flash[:success] = "Account created"       
      redirect_to accounts_path     
    else       
      render 'new'     
    end   
  end
end

来自注册 > edit.html.erb

<% if account_signed_in? %>
  <div class="jumbotron">
    <span><%= link_to "Add user", new_user_registration_path, class: "btn btn-primary btn-sm" %></span>
  </div>
<% end %>

【问题讨论】:

  • 我正在做一个类似的项目,如果你找到了解决方案,我很感兴趣。运气好吗?

标签: ruby-on-rails devise


【解决方案1】:

我不确定您究竟想如何创建用户

但是用

创建一个用户
@account.users.build

会自动将 account_id 添加到用户对象。

希望这会有所帮助! :)

【讨论】:

  • 感谢您的建议!在我的 UsersController 中,我将“@user = User.new”替换为“@account.users.build”,但控制台仍然告诉我 account_id 为 nil。由于它不是自动发生的,因此肯定有一些我无法弄清楚的更大问题。如果您有任何其他建议,我将不胜感激。
  • 我很惊讶!如果帐户在数据库中持续存在,那么“@account.users.build”肯定会在用户对象中添加 account_id!如果@account 是新记录,那么 account_id 可能为 nil,但您的情况似乎并非如此。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多