Ruby - 如何将 ruby​​ gem 集成到我的控制器中?

发布于 2025-01-18 02:01:24 字数 3628 浏览 1 评论 0 原文

我创造了这个宝石> 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 项目中实现它。

我应该在控制器内部的哪个位置添加该方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2025-01-25 02:01:24

我会考虑将该逻辑转移到模型中。

例如,作为a

# in app/models/appointment.rb
def notes=(notes)
  sanitized_notes = Badwordgem::Base.sanitize(notes)
  super(sanitized_notes)
end

或作为

# in app/models/appointment.rb
before_validation :sanitize_notes

private

def sanitize_notes
  self.notes = Badwordgem::Base.sanitize(notes)
end

这两个版本都有一个优势,即无论是如何创建它们,而不仅仅是在这种特定的控制器方法中,都可以确保所有注释都对所有注释进行了消毒。例如,当您通过耙子任务或导轨控制台导入约会时。此外,这使得测试变得更加容易,您可以使用控制器中的默认模式:

@appointment = current_user.appointments.build(appointment_params)
respond_to do |format|
  if @appointment.save
    # ...

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
    # ...
可是我不能没有你 2025-01-25 02:01:24

有趣的是,一旦您发布了如何解决。 。 。

我在创建功能中添加了此内容以过滤坏单词。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文