[英]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.