开发者

Rails proper way to display fields from another model in a view

开发者 https://www.devze.com 2023-03-15 22:13 出处:网络
I have two models, one named Order and the other named Member. In my Order model I have: has_many :members, :dependent => :destroy

I have two models, one named Order and the other named Member. In my Order model I have:

has_many :members, :dependent => :destroy
accepts_nested_attributes_for :members

and in my Member model I have:

belongs_to :order

My members database has a name field thats a string. Now my question is, how do I display that field from my members database in my orders view. Would I use a fields_for tag? Basically what I want is an Order form, but also on the same page to fill out information about the Members for your order and it to save that information to the members database instead of in the orders database.

For example something like this:

Your name: textfield
Member 1 name: textfield
Member 2 name: textfield

How would i do this? is it possible to create more than one name field for the members without having to copy and paste it multiple times such as with a loop?

Thanks开发者_开发知识库 in advance!


Yes, you can use fields_for, for this particular scenario.

There are several Railscasts that make use of fields_for, but this is one of the later ones.

http://railscasts.com/episodes/196-nested-model-form-part-1


From what I can tell, your needs should be pretty well met by cocoon.

0

精彩评论

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

关注公众号