简体   繁体   English

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

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

I try to use Friendly_id but I have an error just for one model. 我尝试使用Friendly_id,但仅对一种模型有错误。

track model : 轨道型号:

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

poi model : poi模型:

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

I need to have a pois list by track 我需要按曲目列出清单

In my pois_controller : 在我的pois_controller中:

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

In my routes : 在我的路线中:

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

But I want to go to my pois_index, the params for track_id is a string (the slug), not an integer. 但是我想去我的pois_index,track_id的参数是一个字符串(段),而不是整数。

What can I do ? 我能做什么 ?

In Rails bad idea to interpolate params into raw SQL. 在Rails中,将参数插值到原始SQL中的想法很糟糕。 That is why you have an error with wrong syntax. 这就是为什么您使用错误的语法会出错。 In this case you should use ActiveRecord. 在这种情况下,您应该使用ActiveRecord。

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

When you interpolate params to sql you should prevent sql injection. 将参数插值到sql时,应防止sql注入。 ActiveRecord do it automatically. ActiveRecord自动执行。

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

相关问题 PG :: InvalidTextRepresentation:错误:整数的无效输入语法 - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer PG :: InvalidTextRepresentation:错误:整数的无效输入语法:“ aaa” - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “aaa” heroku:PG :: InvalidTextRepresentation:错误:整数的无效输入语法:“” - heroku: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “” ActiveRecord::Enum - PG::InvalidTextRepresentation:错误:整数输入语法无效: - ActiveRecord::Enum - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: PG :: InvalidTextRepresentation:ERROR:整数的输入语法无效:“M” - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “M” InvalidTextRepresentation:错误:整数的无效输入语法:“ all” - InvalidTextRepresentation: ERROR: invalid input syntax for integer: “all” Rails迁移 - PG ::错误:错误:整数的输入语法无效:“” - Rails Migration - PG::Error: ERROR: invalid input syntax for integer: “” PG错误:尝试销毁时整数的无效输入语法 - PG ERROR: invalid input syntax for integer when trying to destroy Friendly_Id错误,未初始化常量 - Friendly_Id error, unintialized constant Rails:friendly_id - Rails: friendly_id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM