Ruby - 如何将 ruby gem 集成到我的控制器中?
我创造了这个宝石> https://rubygems.org/gems/badwordgem
这是我的 Rails 项目中的控制器。
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
在我的约会模型架构中,我有“注释”列,这是我想要过滤坏词的地方。
我想将 Badwordgem::Base.sanitize() 集成到我的控制器中,以便在创建约会时可以过滤坏词。
我尝试像这样将其添加到此处,
def create
#@appointment = Appointment.new(appointment_params)
@appointment.notes = Badwordgem::Base.sanitize(@appointment.notes)
@appointment = current_user.appointments.build(appointment_params)
但这会抛出未定义的方法`注释' for nil:NilClass
该 gem 已经通过 IRB 测试并且可以工作。我不知道如何在我自己的 Rails 项目中实现它。
我应该在控制器内部的哪个位置添加该方法?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会考虑将该逻辑转移到模型中。
例如,作为a :
或作为:
这两个版本都有一个优势,即无论是如何创建它们,而不仅仅是在这种特定的控制器方法中,都可以确保所有注释都对所有注释进行了消毒。例如,当您通过耙子任务或导轨控制台导入约会时。此外,这使得测试变得更加容易,您可以使用控制器中的默认模式:
I would consider moving that logic into the model.
For example as a custom setter method:
Or as a
before_validation
: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:
有趣的是,一旦您发布了如何解决。 。 。
我在创建功能中添加了此内容以过滤坏单词。
Funny how once you post you figure it out. . .
I added this inside my create function it to filter the bad words.