找座问题集锦 - tianlu1677/tianlu1677.github.io GitHub Wiki

记录一下自己在修改Bug中容易忽略的点以及错误改正的方法。 记录一下自己在修改Bug中容易忽略的点以及错误改正的方法。

  1. 证书验证的API调用方式
def verify_certificate_code(number)
    # Faraday.post('http://localhost:3006/api/v4/certificates_users/check_certificate_code', { number: '123456'  })
    verified_certificate_url = "http://#{Settings.course_domain}/api/v4/certificates_users/check_certificate_code"
    f = Faraday.post(verified_certificate_url, {number: number})
    f_body = ::JSON.parse(f.body)
    if f_body["status"] == '200'
      true
    else
      false
    end
  end

这个地方使用了 Faraday 来解析了htm了, 根据返回的 status 来判断是否对应成功了。

resources :certificates_users do
    post 'check_certificate_code' do
      params do
        requires :number, type: String, desc: "证书编号"
      end
      @certificates_user = CertificatesUser.find_by(number: params[:number])
      if @certificates_user.present?
        { status: 200 }
      else
        { status: 404 }
      end
    end
end
  1. scope的使用方法,以及结合has_scope 在后台执行搜索的方法

scope一般是用在Model中,has_scope是用在controller中执行搜索功能.要使用has_scope必须要现在model中编写scope,才能使用这个内容。

class Graduation < ActiveRecord::Base
  scope :featured, -> { where(:featured => true) }
  scope :by_degree, -> degree { where(:degree => degree) }
  scope :by_period, -> started_at, ended_at { where("started_at = ? AND ended_at = ?", started_at, ended_at) }
end

class GraduationsController < ApplicationController
  has_scope :featured, :type => :boolean
  has_scope :by_degree
  has_scope :by_period, :using => [:started_at, :ended_at], :type => :hash

  def index
    @graduations = apply_scopes(Graduation).all
    @graduations = Graduation.by_degree
  end
end

而我们这里主要采用的是 执行搜索功能 在model中我们要预先定义搜素条件