Ruby on Rails:gem devise会话已登录

时间:2019-07-15 18:50:36

标签: ruby-on-rails session devise rubygems ruby-on-rails-5

我有两个表,公司和雇员,所以我想在注册一个雇员时,他的注册也适用于已经登录的公司,也就是说,不必选择该雇员的哪个公司。我将如何在红宝石上做到这一点?我正在使用gem devise登录。

2 个答案:

答案 0 :(得分:0)

在用户控制器中尝试以下操作:

    @user = User.new(user_params)
    @user.company_id = current_company.id
    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

答案 1 :(得分:0)

模型公司:

null

模型用户(用户是雇员):

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

公司控制器:

class User < ApplicationRecord

  belongs_to :company

  enum kind: { pattern: 0, admin: 1 }

  has_many :approved

  has_many :expenses

end

用户控制器

    class CompaniesController < ApplicationController
  before_action :set_company, only: [:show, :edit, :update, :destroy]


  def pesquisa
    if params[:q] != nil
    @companies = Company.where("companies.name LIKE '%"+params[:q]+"%'")
    else
      @companies = Company.all
    end
    respond_to do |format|
      format.html
      format.json
    end
  end

  # GET /companies
  # GET /companies.json
  def index
    @companies = Company.all
  end

  # GET /companies/1
  # GET /companies/1.json
  def show
  end

  # GET /companies/new
  def new
    @company = Company.new
  end

  # GET /companies/1/edit
  def edit
  end

  # POST /companies
  # POST /companies.json
  def create
    @company = Company.new(company_params)

    respond_to do |format|
      if @company.save
        format.html { redirect_to @company, notice: 'Company was successfully created.' }
        format.json { render :show, status: :created, location: @company }
      else
        format.html { render :new }
        format.json { render json: @company.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /companies/1
  # PATCH/PUT /companies/1.json
  def update
    respond_to do |format|
      if @company.update(company_params)
        format.html { redirect_to @company, notice: 'Company was successfully updated.' }
        format.json { render :show, status: :ok, location: @company }
      else
        format.html { render :edit }
        format.json { render json: @company.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /companies/1
  # DELETE /companies/1.json
  def destroy
    @company.destroy
    respond_to do |format|
      format.html { redirect_to companies_url, notice: 'Company was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_company
      @company = Company.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def company_params
      params.require(:company).permit(:name, :responsible, :telefone, :email, :password, :password_confirmation)
    end
end