如何使用子模型中的字段来合并父模型?
我想使用 Mongoid 对 Rails 3 中模型的 url 进行 slugify。问题是我想在 slug 中使用的字段位于子模型中。我正在使用 mongoid-slug gem 来找到解决方案,到目前为止我的尝试是这样的:
class Building
references_one :address
def to_param
address.to_param
end
end
class Address
referenced_in :building
field :houseno
field :street
slug :houseno, :street
end
虽然这允许我通过调用 building_path(building)
形成正确的 url,但该页面不包含正确的值。错误消息抱怨对象 id 不正确,我不确定如何让 Rails 监听并通过 to_param 查找记录。
I'd like to slugify the urls for a model in Rails 3, using Mongoid. The problem is the fields I want to use in the slug are located in a child model. I am using mongoid-slug gem to find a solution to this and my attempt so far is this:
class Building
references_one :address
def to_param
address.to_param
end
end
class Address
referenced_in :building
field :houseno
field :street
slug :houseno, :street
end
While this allows me to form the correct url by calling building_path(building)
, the page does not contain the correct values. Error message complains that object id is incorrect, and I'm not sure how to get Rails to listen and find the record by to_param.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于好奇的人来说,这是我解决自己问题的方法。我意识到我需要将显示操作从 更改
为
瞧!有用。
For the curious, here is how I solved my own problem. I realized that I needed to use change the show action from
to
And voila! It works.