Friendly_id 和 ActiveScaffold 冲突

发布于 2024-10-12 22:00:56 字数 1377 浏览 8 评论 0原文

我有 friend_idActiveScaffold 为我的 Rails 应用程序安装。

因为并非所有模型都有唯一的名称字段,所以我必须使用 Slugged Model 使其发挥作用。 Friendly_id 完美地完成了这项工作我有友好的 URL,我可以使用友好的 id 加载对象。

但是当我想用 ActiveScaffold 创建一个新对象时,它显示以下错误消息:

ActiveScaffold::ReverseAssociationRequired (协会蛞蝓:为了 支持 :has_one 和 :has_many 其中 父记录是新的,子记录 记录验证的存在 父级,ActiveScaffold 需要 反向关联(belongs_to)。)

当然,我无法在该侧创建 belongs_to 关联,因为它是由 Friendly_id 模块创建的,并且应该包含每个以 sluged 方式工作的模型那里。

该模型如下所示:

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

有办法让它工作吗?

版本:Friendly_id 3.2.0,ActiveScaffold 是 rails-2.3 git 分支中的最新版本。

更新:看起来在生产模式下并不冲突。

I have friendly_id and ActiveScaffold installed for my Rails application.

Because not all of my models have unique name fields I have to use the Slugged Model to make it work. friendly_id does the job flawlessly I have friendly URLs and I can load the objects using the friendly id.

But when I want to create a new object with ActiveScaffold, it says the following error message:

ActiveScaffold::ReverseAssociationRequired
(Association slugs: In order to
support :has_one and :has_many where
the parent record is new and the child
record(s) validate the presence of the
parent, ActiveScaffold requires the
reverse association (the belongs_to).)

Of course I cannot create the belongs_to association in that side because it's created by the friendly_id module and every model which works slugged way should be included there.

The model looks like this:

class FooBar < ActiveRecord::Base
  has_friendly_id :name, :use_slug => true, :approximate_ascii => true
end

In my 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

Is there a way to make this work?

The versions: friendly_id 3.2.0, ActiveScaffold latest in the rails-2.3 git branch.

UPDATE: Seems like it does not conflict in production mode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

驱逐舰岛风号 2024-10-19 22:00:56

调用

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

,并且它可以工作:-)

PS:我使用Friendly_id作为gem和ActiveScaffold VHO主分支用于导轨 3

calling

has_friendly_id :name, :cache_column => 'cached_slug', :use_slug => true

... creates a has_many and a has one associations pointing to a slug AR model which hasn't any polymorphic belongs to association properly defined.

So basically what you need to do to solve this error is to define the reverse associations in the controller of your parent model (the one who has friendly_id stuff)

  active_scaffold :products do |config|
    ...
    config.columns[:slug].association.reverse = :product
    config.columns[:slugs].association.reverse = :product
  end

and it works :-)

PS : I use friendly_id as gem and ActiveScaffold VHO master branch for rails 3

泅渡 2024-10-19 22:00:56

过去我也有同样的问题,我已经解决了,但我不记得我的解决方案,看看我的代码,唯一相关的黑客是使用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

我是不确定,抱歉,但如果您尝试,请告诉我。

对不起我的英语

In the past I have the same problem , i have solved , but i dont remember my solution , lookin at my code the only relevant hack is to use friendly_id as plugin and load it at last with config.plugin in environemnt.rb

aviable_plugins = Dir.glob(RAILS_ROOT+"/vendor/plugins/*").collect {|i| i.split("/").last }
config.plugins  = aviable_plugins + [:friendly_id] #friendly_id must be last

I'M NOT SURE ,sorry, but if you try let my know.

sorry for my english to

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文