繁体   English   中英

Friendly_id PG :: InvalidTextRepresentation:错误:整数的无效输入语法:

[英]Friendly_id PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer:

我尝试使用Friendly_id,但仅对一种模型有错误。

轨道型号:

class Track < ApplicationRecord 
  extend FriendlyId
  friendly_id :track_name, use: :slugged
  has_many :pois

poi模型:

class Poi < ApplicationRecord
  extend FriendlyId
  friendly_id :name_and_city, use: :slugged
  belongs_to :track

我需要按曲目列出清单

在我的pois_controller中:

def index
  @pois = Poi.all
  @pois = Poi.where("track_id = ?", params[:track_id])

在我的路线中:

 resources :tracks, only:[], :shallow => true  do
   resources :pois
 end

但是我想去我的pois_index,track_id的参数是一个字符串(段),而不是整数。

我能做什么 ?

在Rails中,将参数插值到原始SQL中的想法很糟糕。 这就是为什么您使用错误的语法会出错。 在这种情况下,您应该使用ActiveRecord。

Poi.where(track_id: params[:track_id])

将参数插值到sql时,应防止sql注入。 ActiveRecord自动执行。

暂无
暂无

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

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