开发者

How to slugify a parent model with fields from a child model?

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.


For the curious, here is how I solved my own problem. I realized that I needed to use change the show action from

@building = Building.find(params[:id])

to

@building = Address.find_by_slug(params[:id]).building

And voila! It works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜