繁体   English   中英

Ruby - 如何将 ruby gem 集成到我的 controller 中?

[英]Ruby - how to integrate ruby gem into my controller?

我创建了这个 gem > https://rubygems.org/gems/badwordgem

这是我的 Rails 项目中的 controller。

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

在我的约会模式 model 中,我有“注释”列,这是我想过滤坏词的地方。

我想将 Badwordgem::Base.sanitize() 集成到我的 controller 中,这样我就可以在创建约会时过滤坏词。

我试过像这样在这里添加它

def create
   #@appointment = Appointment.new(appointment_params)
   @appointment.notes = Badwordgem::Base.sanitize(@appointment.notes)
   @appointment = current_user.appointments.build(appointment_params)

但这会为 nil:NilClass 抛出未定义的方法 `notes'

该 gem 已经过 IRB 测试并且可以正常工作。 我不知道如何在我自己的 Rails 项目中实现它。

在我的 controller 中的什么地方添加方法?

我会考虑将该逻辑移至 model。

例如作为自定义 setter 方法

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

或者作为before_validation

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

private

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

这两个版本的优点是它们确保所有笔记都经过清理,无论它们是如何创建的,而不仅仅是在这个特定的 controller 方法中。 例如,当您通过 rake 任务或 Rails 控制台导入约会时。 此外,这使测试更容易一些,您可以像这样在 controller 中使用默认模式:

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

有趣的是,一旦你发帖你就明白了。 . .

我在 create function 里面添加了这个来过滤坏词。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM