Friendly_id 和 ActiveScaffold 冲突
-
13-10-2019 - |
题
我有 友好ID 和 活动脚手架 为我的 Rails 应用程序安装。
因为并非所有模型都有唯一的名称字段,所以我必须使用 重击模型 使其发挥作用。Friendly_id 完美地完成了这项工作我有友好的 URL,我可以使用友好的 id 加载对象。
但是当我想用 ActiveScaffold 创建一个新对象时,它显示以下错误消息:
ActivesCaffold :: ReverseAssociatienquired(协会slugs:为了支持:has_one and:has_many,父母记录是新的,并且子记录验证了父母的存在,accivesCaffold需要反向关联(属于属于ands_to)。
当然我不能创建 belongs_to
在那一边的协会,因为它是由 friendly_id
模块和每个以重击方式工作的模型都应该包含在那里。
该模型如下所示:
class FooBar < ActiveRecord::Base
has_friendly_id :name, :use_slug => true, :approximate_ascii => true
end
在我的 ApplicationController
:
class Admin::FooBarsController < Admin::ApplicationController
active_scaffold :foo_bar do |config|
config.list.columns = [ :id, :name ])
config.update.columns = [ :name ]
config.create.columns = config.update.columns
end
end
有办法让这项工作发挥作用吗?
版本:友好ID 3.2.0
, ,ActiveScaffold最新的 rails-2.3
git 分支。
更新:看起来在生产模式下并不冲突。
解决方案
呼叫
has_friendly_id :name, :cache_column => 'cached_slug', :use_slug => true
...创建一个 has_many 和一个 has one 关联,指向一个 slug AR 模型,该模型没有正确定义任何多态所属关联。
因此,基本上,要解决此错误,您需要做的是在父模型的控制器中定义反向关联(具有Friendly_id内容的控制器)
active_scaffold :products do |config|
...
config.columns[:slug].association.reverse = :product
config.columns[:slugs].association.reverse = :product
end
它有效:-)
附:我使用Friendly_id作为gem和ActiveScaffold VHO master分支用于rails 3
其他提示
过去我也有同样的问题,我已经解决了,但我不记得我的解决方案,看看我的代码,唯一相关的黑客是使用Friendly_id作为插件,最后使用environemnt.rb中的config.plugin加载它
aviable_plugins = Dir.glob(RAILS_ROOT+"/vendor/plugins/*").collect {|i| i.split("/").last }
config.plugins = aviable_plugins + [:friendly_id] #friendly_id must be last
我不确定,抱歉,但如果你尝试让我知道。
对不起我的英语