在我的模型中,我有以下内容:
class Dyno < ActiveRecord::Base
include FriendlyId
friendly_id :slug_candidates, :use => [:slugged, :history, :finders]
def should_generate_new_friendly_id?
car.present? && horsepower_changed?
end
def slug_candidates
[
[car.owner, :horsepower, "horsepower-dyno"],
[car.owner, car.nickname, :horsepower, "horsepower-dyno"]
]
end
当我尝试更新记录时: 在生成一个slug之前,我运行我的验证并获得所有验证消息没有问题,但是一旦生成了slug,我点击save我得到404.即使我输入了所有正确的字段,然后点击保存将通过验证,它不保存记录并返回404
看起来它似乎试图通过新的“-slug-”查找并保存记录,然后记录中存在该slug。
Started PATCH "/dynos/john-smith-1800-horsepower-dyno" for 127.0.0.1 at 2014-03-09 12:50:03 -0400
Processing by DynosController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rxz8PPyTYgQx6umBB7t50fLtPUk4zGrCmSiwd751jxI=", "dyno"=>{"car_id"=>"1", "horsepower"=>"1800", "torque"=>"555", "boost"=>"", "nitrous"=>"no", "nitrous_amount"=>"", "fuel_id"=>"2", "tuner"=>"", "video_url"=>"", "dyno_brand_id"=>"", "details"=>""}, "commit"=>"Update Dyno", "id"=>"john-smith-1800-horsepower-dyno"}
[1m[35mDyno Load (0.6ms)[0m SELECT "dynos".* FROM "dynos" INNER JOIN "friendly_id_slugs" ON "friendly_id_slugs"."sluggable_id" = "dynos"."id" AND "friendly_id_slugs"."sluggable_type" = 'Dyno' WHERE ("friendly_id_slugs"."sluggable_type" = 'Dyno' AND "friendly_id_slugs"."slug" = 'john-smith-1800-horsepower-dyno') ORDER BY "dynos"."id" ASC LIMIT 1
Completed 404 Not Found in 5ms
在我的控制器中,我正在使用.friendly范围
def set_dyno
@dyno = Dyno.friendly.find(params[:id])
end
此外,在我的控制器中,我在新操作中进行保存,以便我可以让用户从同一个新视图中添加相关图片。也许Friendly_Id可能会对现有记录中的nil slug做出反应?
def new @dyno = Dyno.new @dyno.save :validate => false end
我做错了什么?
我正在使用friendly_id 5.0.2
答案 0 :(得分:0)
请从Dyno中删除友情。
friendly.find(params[:id])
因为你在这里使用finder
friendly_id :slug_candidates, :use => [:slugged, :history, :finders]