不能在轨道5.1.2项目上使用Milia

时间:2017-07-11 20:28:37

标签: ruby-on-rails ruby

我正在尝试使用milia gem设置多租户rails应用程序。

当我尝试注册新用户时,我收到此错误:

MILIA >>>>> [register user/org] tenant create failed - Tenant must exist

即使我尝试从控制台创建新用户或新帐篷,我也会收到相同的错误“租户必须存在”。

在轨道5我知道我应该注意模特中的关系,但所有模型都被设置为他们的演示节目。

编辑1:

视图/设计/注册/ new.html.erb

<div class="panel panel-default devise-bs">
  <div class="panel-heading">
    <h4><%= t('.sign_up', default: 'Sign up') %></h4>
  </div>
  <div class="panel-body">
    <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { role: 'form' }) do |f| %>
      <div class="form-group">
        <%= f.label :email %>
        <%= f.email_field :email, autofocus: true, class: 'form-control' %>
      </div>
      <div class="form-group">
        <%= f.label :password %>
        <%= f.password_field :password, class: 'form-control' %>
      </div>
      <div class="form-group">
        <%= f.label :password_confirmation %>
        <%= f.password_field :password_confirmation, class: 'form-control' %>
      </div>
      <%= fields_for (:tenant) do |w| %>
        <div class="form-group col-md-6 no-left-padding">
          <%= w.label :name, 'Organization' %>
          <span class="description">(Unique name for your group)</span>
          <%= w.text_field :name, required: true, class: 'form-control' %>
        </div>
        <div class="form-group col-md-6 no-right-padding">
          <%= w.label :plan%>
          <%= w.select :plan, options_for_select(Plan.options, params[:plan]) ,{}, required: true, class: 'form-control' %>
        </div>
      <% end %>
      <%= f.submit t('.sign_up', default: 'Sign up'), class: 'btn btn-primary' %>
    <% end %>
  </div>
</div>
<%= render 'devise/shared/links' %>

应用程序/模型/ member.rb

class Member < ApplicationRecord

  belongs_to :user
  acts_as_tenant

  DEFAULT_ADMIN = {
    first_name: "Admin",
    last_name:  "Please edit me"
  }

  def self.create_new_member(user, params)
    # add any other initialization for a new member
    return user.create_member( params )
  end

  def self.create_org_admin(user)
    new_member = create_new_member(user, DEFAULT_ADMIN)
    unless new_member.errors.empty?
      raise ArgumentError, new_member.errors.full_messages.uniq.join(", ")
    end

    return new_member

  end

end

应用程序/模型/ tenant.rb

class Tenant < ApplicationRecord

   acts_as_universal_and_determines_tenant
  has_many :members, dependent: :destroy

  validates :name, uniqueness: true, presence: true
  validates :plan, presence: true
  # validates_uniqueness_of :name
  # validates_presence_of :name
  # validates_presence_of :plan

    def self.create_new_tenant(tenant_params, user_params, coupon_params)

      # tenant = Tenant.new(:name => tenant_params[:name], :plan => tenant_params[:name])
        tenant = Tenant.new(tenant_params)

      if new_signups_not_permitted?(coupon_params)

        raise ::Milia::Control::MaxTenantExceeded, "Sorry, new accounts not permitted at this time"

      else
        tenant.save    # create the tenant
      end
      return tenant
    end

  # ------------------------------------------------------------------------
  # new_signups_not_permitted? -- returns true if no further signups allowed
  # args: params from user input; might contain a special 'coupon' code
  #       used to determine whether or not to allow another signup
  # ------------------------------------------------------------------------
  def self.new_signups_not_permitted?(params)
    return false
  end

  # ------------------------------------------------------------------------
  # tenant_signup -- setup a new tenant in the system
  # CALLBACK from devise RegistrationsController (milia override)
  # AFTER user creation and current_tenant established
  # args:
  #   user  -- new user  obj
  #   tenant -- new tenant obj
  #   other  -- any other parameter string from initial request
  # ------------------------------------------------------------------------
    def self.tenant_signup(user, tenant, other = nil)
      #  StartupJob.queue_startup( tenant, user, other )
      # any special seeding required for a new organizational tenant
      #
      Member.create_org_admin(user)
      #
    end


end

应用程序/模型/ user.rb

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

  acts_as_universal_and_determines_account
  has_one :member, :dependent => :destroy

end

2 个答案:

答案 0 :(得分:4)

如果你像我一样正在运行rails 5.1解决了我的问题就是删除:

SELECT *
FROM Table
WHERE DateTimeColumn < dateadd(day,1,cast(getdate() as date))
config / application.rb

中的

答案 1 :(得分:0)

我会在会员班中将act_as_tenant替换为acts_as_universal_and_determines_account