如何在Rails表单中处理多个模型 [英] how to handle multiple models in a rails form

查看:70
本文介绍了如何在Rails表单中处理多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://weblog.rubyonrails.org/2009/1/26/nested-model-forms

这篇文章有助于学习如何以Rails形式处理多个模型.只要模型是嵌套的,它就起作用.如果不是,那该怎么办?可以说,我有一个表格,用户可以在其中填写个人详细信息,地址详细信息和一堆指定她的兴趣的复选框.单一表格中至少包含3个表格,而没有3个不同的保存按钮的最佳处理方式是什么?

This post helped in learning how to handle multiple models in a rails form. It works as long as the models are nested. what if they are not? lets say, I have a form, where the user fills personal details, address details and a bunch of checkboxes specifying her interests. There are at least 3 tables involved in this one single form, what is the best way to handle this, without having 3 different save buttons?

推荐答案

两个选项:

首先是 ActivePresenter ,它对此非常有用.

First is ActivePresenter which works well for this.

第二个只是使用fields_for:

<%= form_for @user do |f| %>

   <%=f.label :name %>
   <%=f.text_field :name %>

   <%= fields_for @address do |fa| %>
      <%=fa.label :city %>
      <%=fa.text_field :city %>
   <% end %>

<% end %>

然后在控制器中,保存记录.

Then in the controller, save the records.

 @user = User.new(params[:user]) 
 @address = Address.new(params[:address])

ActivePresenter效果很好.

ActivePresenter works so well though.

还通过Google找到了 railsforum帖子

Also found a railsforum post via Google, which would work well.

这篇关于如何在Rails表单中处理多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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