故障排除friendly_id w / Rails 4:在rails控制台中按预期工作,从控制器获取RecordNotFound错误

我已经成功地在我的Rails 4应用程序中使用了带有多个模型的friendly_id。 我之前有两个我决定合并的模型(终端和位置)。 终端显示行动以前使用的是friendly_id,在那里我可以访问/终端/奥尔巴尼并使用slug’albany’获得终端。

我已经通过大部分重构压缩到只有Locations,并且在最后一块(修复视图)。 一路上我一直在Rails控制台中测试并使用rspec,事情似乎按预期工作。 还有其他模型仍然可以按预期工作。

现在我正在为Locations的新显示页面工作,我在尝试访问/ locations / albany时遇到以下错误:

ActiveRecord::RecordNotFound in LocationsController#show Couldn't find Location with id=albany 

但是,当我访问/ locations / 5时,它成功加载了记录。 它也适用于Rails控制台:

 Location.find(5) # returns the correct location object Location.find(5).slug # returns 'albany' Location.friendly.find('albany') # returns the correct location object 

当我查看Rails dbconsole中生成的slugs时,它们看起来都是正确生成的并且没有重复项。

这是我的模特:

 Class Location < ActiveRecord::Base extend FriendlyId friendly_id :slug_candidates, use: [:slugged, :history] def slug_candidates [ :name, [:name, :zip], [:name, :zip, :location_type], ] end end 

这是我在控制器中的show动作:

 def show @locations = Location.friendly.find(params[:id]) end 

我很难过! 任何提示?

你试过添加:finders?

 friendly_id :slug_candidates, use: [:slugged, :history, :finders]