继承资源和 Mongoid
有没有人成功拥有 Rails 3、Mongoid 和继承资源< /a> 工作吗?有什么技巧可以让它发生吗?我很想使用这两种宝石。
目前我遇到了:
undefined method `scoped'
关于索引操作。
谢谢!
顺便说一句,范围问题的解决方法是像这样覆盖集合:
class CampaignsController < InheritedResources::Base
def collection
@campaigns ||= end_of_association_chain.paginate(:page => params[:page])
end
end
但我正在寻找一种更全面的方法
Has anyone had success having Rails 3, Mongoid and Inherited Resources working? Any tips for making it happen? I would love to use both gems.
Currently I am running into:
undefined method `scoped'
On index actions.
Thanks!
BTW a workaround for the scoped issue is to override collection like so:
class CampaignsController < InheritedResources::Base
def collection
@campaigns ||= end_of_association_chain.paginate(:page => params[:page])
end
end
But I am looking for a more holistic approach
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只使用 mongoid,您应该做的是覆盖继承资源中的默认集合行为。默认行为是这样的:
https://github.com/josevalim/ herited_resources/blob/master/lib/inherited_resources/base_helpers.rb#L22-24
也就是说,以下操作应该可以解决问题:
您甚至可以默认集合分页,并在所有页面中免费分页。
If you are using only mongoid, what you should do is to overwrite the default collection behavior in Inherited Resources. The default behavior is this:
https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/base_helpers.rb#L22-24
That said, the following should do the trick:
You can even default the collection to paginate and have pagination for free in all pages.
或者,您可以修补 Mongoid:
这将使
inherit_resources
方法按预期工作。Alternatively you can patch Mongoid:
This will make
inherit_resources
method work as expected.以下是我为涵盖从
InheritedResources::Base
继承和使用inherit_resources
语句所做的操作。您通常将其放入初始化程序中(我使用 config/initializers/mongoid.rb)。
使
Mongoid 2.0.0.beta.20
和inherited_resources 1.2.1
友好。Here's what I did to cover both inheriting from
InheritedResources::Base
and usinginherit_resources
statement.You typically put this into an initializer (I use
config/initializers/mongoid.rb
).Makes
Mongoid 2.0.0.beta.20
andinherited_resources 1.2.1
friendly.非常有帮助的帖子!
如果您的控制器无法从
InheritedResource::Base
派生子类,而是必须使用类方法inherit_resources
,您会如何执行此操作,如下所示:上面的猴子补丁不似乎在这个设置中工作。
看起来关键可能是
InheritedResources::Base.inherit_resources
但我不清楚覆盖此方法的正确方法。如果我在这里走错了路,请纠正。Very helpful post !
How would you do this if your controller cannot be subclassed from
InheritedResource::Base
but rather you have to use the class methodinherit_resources
, like so :the above monkey patch does not seem to work in this setup.
It looks like the key might be
InheritedResources::Base.inherit_resources
but i am unclear on the correct way to overwrite this method. Please correct if i am on the wrong path here.