0

I am trying to create a new tenant after the successful creation of a new shop. So I have a subdomain field in my shop model. I also added after_filter method to create a new tenant but it is throwing me some error. My shop model looks like this

class ShopsController < ApplicationController
    before_action :set_shop, except: [:new,:create, :index, :catalog]
    before_action :verify_owner, only: [:edit,:update]
    before_action :only_one_shop_with_free_account, only: [:create,:new]
    after_action :create_tenant, only: [:create]
    after_action :delete_tenant, only: [:destroy]


    layout 'layouts/storelayout.html.erb'

    def index
    end

    def new
        @shop = Shop.new
    end

    def create
        @shop = Shop.new(shop_params)
        @shop.user_id = current_user.id
        if @shop.save
            flash[:notice] = "You have successfully created your first shop"
            redirect_to @shop
        else
            flash[:error] = "You cannot create a shop"
            redirect_to @shop
        end
    end

    def show
        @products = @shop.products.all
        @owner = User.find(@shop.user_id)
    end

    def edit
    end

    def update
        if @shop.update_attributes(shop_params)
            redirect_to shop_path
            flash[:notice] = "Changes updated successfully"
        else
            redirect_to shop_path
            flash[:alert] = "Something went wrong"
        end
    end

    def catalog
        @shop = Shop.find(1)
    end

    private 

    def shop_params
        params.require(:shop).permit(:name, :description, :location, :img_url, :cover_img_url, :phone, :subdomain)
    end

    def set_shop
        @shop = Shop.friendly.find(params[:id])
    end

    def verify_owner
        if user_signed_in?
            if @shop.user_id == current_user.id
                return true
            else
                return false
            end
        end
    end

    def only_one_shop_with_free_account
        if current_user.shop.present?
            redirect_to dashboard_path
            flash[:alert] = "You cannot create multiple shops with free account"
        end
    end

    def create_tenant
        Apartment::Tenant.create(subdomain)
    end

    def delete_tenant
        Apartment::Tenant.drop(subdomain)
    end

end

But it gives me the following error

NoMethodError (undefined method `create_tenant' for #<ShopsController:0x0000000576a5e8>):

My apartment.rb file looks like this

    # You can have Apartment route to the appropriate Tenant by adding some Rack middleware.
# Apartment can support many different "Elevators" that can take care of this routing to your data.
# Require whichever Elevator you're using below or none if you have a custom one.
#
# require 'apartment/elevators/generic'
# require 'apartment/elevators/domain'
require 'apartment/elevators/subdomain'
# require 'apartment/elevators/first_subdomain'

#
# Apartment Configuration
#
Apartment.configure do |config|

  # Add any models that you do not want to be multi-tenanted, but remain in the global (public) namespace.
  # A typical example would be a Customer or Tenant model that stores each Tenant's information.
  #
   config.excluded_models = ["Shop", "User", "AdminUser"]

  # In order to migrate all of your Tenants you need to provide a list of Tenant names to Apartment.
  # You can make this dynamic by providing a Proc object to be called on migrations.
  # This object should yield either:
  # - an array of strings representing each Tenant name.
  # - a hash which keys are tenant names, and values custom db config (must contain all key/values required in database.yml)
  #
  # config.tenant_names = lambda{ Customer.pluck(:tenant_name) }
  # config.tenant_names = ['tenant1', 'tenant2']
  # config.tenant_names = {
  #   'tenant1' => {
  #     adapter: 'postgresql',
  #     host: 'some_server',
  #     port: 5555,
  #     database: 'postgres' # this is not the name of the tenant's db
  #                          # but the name of the database to connect to before creating the tenant's db
  #                          # mandatory in postgresql
  #   },
  #   'tenant2' => {
  #     adapter:  'postgresql',
  #     database: 'postgres' # this is not the name of the tenant's db
  #                          # but the name of the database to connect to before creating the tenant's db
  #                          # mandatory in postgresql
  #   }
  # }
  # config.tenant_names = lambda do
  #   Tenant.all.each_with_object({}) do |tenant, hash|
  #     hash[tenant.name] = tenant.db_configuration
  #   end
  # end
  #
  config.tenant_names = lambda { Shop.pluck :subdomain }

  # PostgreSQL:
  #   Specifies whether to use PostgreSQL schemas or create a new database per Tenant.
  #
  # MySQL:
  #   Specifies whether to switch databases by using `use` statement or re-establish connection.
  #
  # The default behaviour is true.
  #
  # config.use_schemas = true

  #
  # ==> PostgreSQL only options

  # Apartment can be forced to use raw SQL dumps instead of schema.rb for creating new schemas.
  # Use this when you are using some extra features in PostgreSQL that can't be represented in
  # schema.rb, like materialized views etc. (only applies with use_schemas set to true).
  # (Note: this option doesn't use db/structure.sql, it creates SQL dump by executing pg_dump)
  #
  # config.use_sql = false

  # There are cases where you might want some schemas to always be in your search_path
  # e.g when using a PostgreSQL extension like hstore.
  # Any schemas added here will be available along with your selected Tenant.
  #
  # config.persistent_schemas = %w{ hstore }

  # <== PostgreSQL only options
  #

  # By default, and only when not using PostgreSQL schemas, Apartment will prepend the environment
  # to the tenant name to ensure there is no conflict between your environments.
  # This is mainly for the benefit of your development and test environments.
  # Uncomment the line below if you want to disable this behaviour in production.
  #
  # config.prepend_environment = !Rails.env.production?
end

# Setup a custom Tenant switching middleware. The Proc should return the name of the Tenant that
# you want to switch to.
# Rails.application.config.middleware.use Apartment::Elevators::Generic, lambda { |request|
#   request.host.split('.').first
# }

# Rails.application.config.middleware.use Apartment::Elevators::Domain
Rails.application.config.middleware.use Apartment::Elevators::Subdomain
# Rails.application.config.middleware.use Apartment::Elevators::FirstSubdomain

Please help me how to resolve this issue. I could not find the solution that worked for me.

Rabin Poudyal
  • 717
  • 6
  • 20
  • I can't see anything that would cause this error. Can you please do some sanity checks, such as: Make sure that the file is saved, you're testing against the correct version (i.e. the right local copy), you don't have multiple `ShopsController` defined, .... ? – Tom Lord Oct 12 '17 at 11:13
  • To clarify, is the error you've shown above caused by **hitting the `ShopsController#create` action**? You're not trying something weird like calling `ShoppingController.create_tenant` from somewhere else in the application? – Tom Lord Oct 12 '17 at 11:15
  • no bro. but when i do something like Apartment::Tenant.create('subdomain') it does the right thing. Right thing I mean it creates subdomain tenant. But I want the variable subdomain to be replaced by value of the column and not the 'subdomain' as string itself. – Rabin Poudyal Oct 12 '17 at 11:21
  • What exact action are you triggering that causes the above error? Are you running something in the console? Running a background job? Clicking a link? (What link?) Can you post the full stack trace? – Tom Lord Oct 12 '17 at 11:25
  • Thanks bro . I figured out the problem. Actually I should add Apartment::Tenant.create(@shop.subdomain) . – Rabin Poudyal Oct 13 '17 at 16:13
  • You did not describe the question properly. That answer does not correspond to your question. – Tom Lord Oct 14 '17 at 07:54

0 Answers0