English 中文(简体)
铁路3个多形态协会,见网页
原标题:Rails 3 polymorphic association, view pages

审议以下多变关系

class Person
  has_many :photos, :as => :attachable
end

class Animal
  has_many :photos, :as => :attachable
end

class Photo
  belongs_to :attachable, :polymorphic => true
end

The view pages of a Person or Animal object contains a gallery of their respective photos. The pages contain the following code:

...
  <%= link_to(image_tag(photo.image_url(:thumb).to_s), [@attachable, photo]) %>
...

然而,在我的照片的页面中,我想根据它是否属于个人或动物提供不同的信息。 例如,如果是个人,它就应当显示其身高、ets子清单;如果是动物,它就应当展示其繁殖、所有人等。

这样做的最佳解决办法是什么?

我可以考虑两种可能的解决办法:

One way could be to direct to a separate controller action in the photo for each class. Something like

<%= link_to(image_tag(photo.image_url(:thumb).to_s), show_person_path[@attachable, photo]) %>
<%= link_to(image_tag(photo.image_url(:thumb).to_s), show_animal_path[@attachable, photo]) %>

但 我认为,这种做法在多吗?

另一种做法是在每个人和动物阶层采取控制者行动,展示相应的信息。 彩礼应当与这一行动而不是上述行动联系起来,而且行动应当提供照片。

但 我感到,正如所展示的相应信息一样,对于每个阶层来说,没有什么值得自己采取行动。 也许帮助者足够了,但我不相信正确的做法(如果上述假设是真实的,请更正我)。

最佳回答

如果动物与人之间的差别不能成为单独控制者的理由,那么你可以有一位控制者,也可以有一位控制者,在为食堂采取行动时,仅仅根据你可爱的类型提出不同的看法:

if @attachable.is_a? Person
  render  gallery_person 
elsif @attachable.is_a? Animal
  render  gallery_animal 
end

我在这里看到的唯一倒数是,如果你引进一种新形式的附加物,你就必须回头并扩展逻辑。 你们可以添加一种不实的观点来缓解这种情况。

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签