I have two tables, company and employee, so I would like when I register an employee, his registration was also for the company that was already logged, that is, without having to have to choose which company of that employee. How would I do this in ruby on rails? I'm using gem devise to login.
Try this in your User Controller:
@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
MODEL COMPANY:
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
MODEL USER (the user is the employee):
class User < ApplicationRecord
belongs_to :company
enum kind: { pattern: 0, admin: 1 }
has_many :approved
has_many :expenses
end
COMPANY CONTROLLER:
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
USER CONTROLLER
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
def pesquisa
if params[:q] != nil
@users = User.where("users.name LIKE '%"+params[:q]+"%'")
else
@users = User.all
end
respond_to do |format|
format.html
format.json
end
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
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
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :cpf, :kind, :timeid, :company_id)
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.