设计中的多用户+ Rails

时间:2015-07-09 12:20:19

标签: ruby-on-rails ruby devise

我想创建多用户应用程序。管理员用户可以创建新用户。我怎么能用设计做到这一点。因为在登录后作为管理员用户我想添加新的用户设计显示错误“你已经登录”。我如何使用设计做到这一点。

我能够创建管理员用户并登录

用户控制器

class UsersController < ApplicationController
  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def edit
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "Successfully created User."
      redirect_to root_path
    else
      render :action => 'new'
    end
  end

 def user_params
    params.require(:user).permit(:email, :username, :password, :password_confirmation,:propic)
  end
end

管理控制器

class ClientsController < ApplicationController
  skip_before_filter :authenticate_user!, only: [:index, :new, :create]

  def new
    @client = Client.new
    @client.build_owner

    render layout: 'sign'
  end

  def index
    @clients = Client.all

    render layout: 'welcome'
  end

  def create
    @client = Client.new(client_params)
    if @client.valid? then
      Apartment::Tenant.create(@client.subdomain)
      Apartment::Tenant.switch(@client.subdomain)
      @client.save
      redirect_to new_user_session_url(subdomain: @client.subdomain)
    else
      render action: 'new'
    end
  end

  private

  def client_params
    params.require(:client).permit(:name, :subdomain, owner_attributes: [:email, :username, :password, :password_confirmation,:propic])
  end
end

应用程序控制器

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :load_tenant
  before_filter :authenticate_user!

  #rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

  private

  def record_not_found
    render 'record_not_found'
  end

  def load_tenant
    Apartment::Tenant.switch(nil)
    return unless request.subdomain.present?

    client = Client.find_by(subdomain: request.subdomain)
    if client then
      Apartment::Tenant.switch(request.subdomain)
    else
      redirect_to root_url(subdomain: false)
    end
  end

  def after_signout_path_for(resource_or_scope)
    new_user_session_path
  end
end

任何?我是Ruby on Rails的新手。所有代码都是大量审判和错误的结果。

0 个答案:

没有答案