I have created this gem > https://rubygems.org/gems/badwordgem
This is my controller inside my rails project.
class AppointmentsController < ApplicationController
before_action :set_appointment, only: %i[ show edit update destroy ]
#before we run anything if the user is not signed in show index and show functions
before_action :authenticate_user!, except: [:index,:show]
#only the correct user can edit,update and destroy
before_action :correct_user, only: [:edit, :update , :destroy]
# GET /appointments or /appointments.json
def index
@appointments = Appointment.all.decorate
end
# GET /appointments/1 or /appointments/1.json
def show
end
# GET /appointments/new
def new
#@appointment = Appointment.new
@appointment = current_user.appointments.build
end
# GET /appointments/1/edit
def edit
end
#function to allow for search functionality
def search
@appointments = Appointment.where("date LIKE?", "%" params[:q] "%")
end
# POST /appointments or /appointments.json
def create
#@appointment = Appointment.new(appointment_params)
@appointment = current_user.appointments.build(appointment_params)
respond_to do |format|
if @appointment.save
format.html { redirect_to appointment_url(@appointment), notice: "Appointment was successfully created." }
format.json { render :show, status: :created, location: @appointment }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /appointments/1 or /appointments/1.json
def update
respond_to do |format|
if @appointment.update(appointment_params)
format.html { redirect_to appointment_url(@appointment), notice: "Appointment was successfully updated." }
format.json { render :show, status: :ok, location: @appointment }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /appointments/1 or /appointments/1.json
def destroy
@appointment.destroy
respond_to do |format|
format.html { redirect_to appointments_url, notice: "Appointment was successfully destroyed." }
format.json { head :no_content }
end
end
#function here that restricts editing so the current logged in user can edit only their records
def correct_user
@appointment = current_user.appointments.find_by(id: params[:id])
redirect_to appointments_path, notice:"NOT ALLOWED TO EDIT THIS" if @appointment.nil?
end
private
# Use callbacks to share common setup or constraints between actions.
def set_appointment
@appointment = Appointment.find(params[:id])
end
# Only allow a list of trusted parameters through.
def appointment_params
params.require(:appointment).permit(:barber, :customer, :notes, :date,:user_id)
end
end
In my schema for the appointment model I have the column 'notes' which is where I want to filter bad words.
I want to integrate Badwordgem::Base.sanitize() into my controller so I can filter bad words when I am creating the appointment.
I've tried adding it here like so
def create
#@appointment = Appointment.new(appointment_params)
@appointment.notes = Badwordgem::Base.sanitize(@appointment.notes)
@appointment = current_user.appointments.build(appointment_params)
but that throws undefined method `notes' for nil:NilClass
The gem has been tested with IRB and works. I am at a loss as to how to implement it inside my own rails project.
Where inside my controller do I add the method?
CodePudding user response:
I would consider moving that logic into the model.
For example as a custom setter method:
# in app/models/appointment.rb
def notes=(notes)
sanitized_notes = Badwordgem::Base.sanitize(notes)
super(sanitized_notes)
end
Or as a before_validation
:
# in app/models/appointment.rb
before_validation :sanitize_notes
private
def sanitize_notes
self.notes = Badwordgem::Base.sanitize(notes)
end
Both versions have the advantage that they make sure all notes are sanitized no matter how they are created and not just in this specific controller method. For example when you import Appointments via a rake task or the Rails console. Additionally, this makes testing a bit easier and you can use the default pattern in the controller like this:
@appointment = current_user.appointments.build(appointment_params)
respond_to do |format|
if @appointment.save
# ...
CodePudding user response:
Funny how once you post you figure it out. . .
I added this inside my create function it to filter the bad words.
def create
#@appointment = Appointment.new(appointment_params)
@appointment = current_user.appointments.build(appointment_params)
@appointment.notes = Badwordgem::Base.sanitize(@appointment.notes)
respond_to do |format|
if @appointment.save
format.html { redirect_to appointment_url(@appointment), notice: "Appointment was successfully created." }
format.json { render :show, status: :created, location: @appointment }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end