notes use rails 5.2 and postgresql
I have Foluser model contains name,email,password,id_watch
I need when admin add new foluser
generate
passwordwhen admin create new foluser generate
passwordlike Secure Password Generatorget
id_watchfromadminmodel and put it toid_watchfromFolusermodelAdmin
when register enterusername,email,password,id_watch`in point 2 need take this id_watch and save it in
usermodel .
admin only create foluser
`
class FolusersController < ApplicationController
before_action :set_foluser, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show, :new , :create, :edit]
# GET /folusers
# GET /folusers.json
def index
@folusers = current_master.foluser.all
#render json: @folusers
end
# GET /folusers/1
# GET /folusers/1.json
def show
#@folusers = Foluser.where(master_id: @master.id).order("created_at DESC")
#@foluser = Foluser.find(params[:id])
#render json: @foluser
end
# GET /folusers/new
def new
@foluser = current_master.foluser.build
end
# GET /folusers/1/edit
def edit
#render json: @foluser
end
# POST /folusers
# POST /folusers.json
def create
@foluser = current_master.foluser.build(foluser_params)
respond_to do |format|
if @foluser.save
format.html { redirect_to @foluser, notice: 'Foluser was successfully created.' }
format.json { render :show, status: :created, location: @foluser }
else
format.html { render :new }
format.json { render json: @foluser.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /folusers/1
# PATCH/PUT /folusers/1.json
def update
respond_to do |format|
if @foluser.update(foluser_params)
format.html { redirect_to @foluser, notice: 'Foluser was successfully updated.' }
format.json { render :show, status: :ok, location: @foluser }
else
format.html { render :edit }
format.json { render json: @foluser.errors, status: :unprocessable_entity }
end
end
end
# DELETE /folusers/1
# DELETE /folusers/1.json
def destroy
@foluser.destroy
respond_to do |format|
format.html { redirect_to folusers_url, notice: 'Foluser was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_foluser
@foluser = Foluser.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def foluser_params
params.require(:foluser).permit(:name, :email, :numberphone, :password)
end
end
foluser model
class Foluser < ApplicationRecord
belongs_to :admin, :optional => true
end
admin model
class Master < ApplicationRecord
has_many :foluser
end
passwordin plain text??!! If so, that's a big security vulnerability.ruby-on-rails-3andruby-on-rails-4? Which version are you actually using? (Is it actuallyv5.x?)Adminfrom which you wish to set theid_watch? Possibly you've mis-described the situation, and it's actually theadmin'sidthat you wish to set as thefoluser'sid_watch? (In which case, I'd rather use a more conventional and descriptive column name, likecreator_id.password". What rules/format do you want to apply to the random string? (Any character/length requirements?) Do you want to reference the value later, e.g. in an email? Should this be considered a permanent, or temporary password? As mentioned above, should this be encrypted (probably!), or do you have a good reason to store the password unencrypted?