编辑"我怎么能在与QUOT从地图数据的关联;在Rails的形式? [英] How can I map data from associations in "Edit" Forms in Rails?

查看:130
本文介绍了编辑"我怎么能在与QUOT从地图数据的关联;在Rails的形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续<一个href=\"http://stackoverflow.com/questions/8752859/how-to-i-serve-data-from-an-objects-associations-in-rails-forms\">How以我服务于轨道的形式从一个对象的数据关联?

我不知道,我想用预先加载,以服务一篇文章的编辑页面对象,其中通过关联的表保存在标签(以及其他属性。在previous问题时,我被告知我可以使用协会的积极加载至pre-载该数据。IE控制器我充实到.find调用包含

I am wondering, I would like to use eager loading to serve to an edit page of an Article object the tags (as well as other attributes that are saved through associated tables. In the previous question, I was informed that I could use eager loading of associations to pre-load that data. IE in the controller I augment the .find call to include

 @article = Article.find(params[:id], :include => [:tags])

这将是伟大的,除了它有两个主要的注意事项。首先是它的形式提供的对象的字段。所以,在这个例子中,:在编辑表单标签领域将与填充:

This would be great except it has two main caveats. First is that it serves the object to the field in the form. So, in this example, the :tags field in the edit form would be populated with:

[#<Tag id: 298, name: "Wondering">]

而不是

Wondering

第二个需要注意的是,我不能为这些协会自定义字段。例如,而不是有:标签字段我有一个:tag_list场。在创作的一篇文章中,用户输入一些标签在这里。在编辑的:标签不要被名称映射并投入tag_list领域。我总是可以将此行添加到控制器:

The second caveat is that I can't serve these associations to custom fields. For example, rather than have a :tags field I have a :tag_list field. On creation of an Article, users input a number of tags here. On edit, the :tags don't get mapped by name and put into the tag_list field. I could always add this line to the controller:

@article.tag_list = @article.tags.map(&:name)

但我在寻找一个更好的解决方案,因为我有很多字段,我会写这一点,它看起来像$ C $行C的浪费。

But I'm looking for a more elegant solution, because I have very many fields for which I would have to write this, and it seems like a waste of lines of code.

任何人都可以帮忙吗?

推荐答案

看看这个Railscast上的虚拟属性的。我是pretty确认这将你的生活变得更加容易。

Check out this Railscast on Virtual Attributes. I'm pretty sure this will your life much easier.

其实,这是pretty多少您的问题完全一样。干得好赖安·贝茨。

Actually, it's pretty much your problem exactly. Good job Ryan Bates.

这篇关于编辑&QUOT;我怎么能在与QUOT从地图数据的关联;在Rails的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆