1 一对多

Using Strong Parameters With Fields For & Nested Forms in Rails 4
http://www.rubyexperiments.com/using-strong-parameters-with-nested-forms/
class Account < ActiveRecord::Base
  has_many :people
  accepts_nested_attributes_for :people
end
class Person < ActiveRecord::Base
  belongs_to :account
end
class AccountsController < ApplicationController
  def new
    @account = Account.new
     @account.people.build  (一对多这样写)          http://www.tuicool.com/articles/Jjee6v (rails 中 create, new, build, save 的用法)
  @account.build_people (一对一这样写)
  end
  def create
    @account = Account.new(new_account_params)
    if @account.save
      respond_to do |format|
        format.html { redirect_to root_path, notice: "Account created successfully." }
      end
    end
  end

  private

  def new_account_params
    params.require(:account).permit( :id, :name, people_attributes: [:id, :email, :password, :password_confirmation] )
  end
end

 <% form_for @accountdo |f| %>
    <%= f.text_field :name %>
    <% f.fields_for :people do |pf| %> #注意这里
      <%= pf.text_field :email %>
    <% end %>
 <% end %>

fields_for的更多相关文章

  1. rails中一个窗体多个模型——fields_for

    详细参考 http://railscasts.com/episodes/73-complex-forms-part-1中part-1.2.3部分 借助field_for可以生成表单来处理两个或更多模型 ...

  2. 使用text存储hash类型的数据 Use text filed to store the hash map

    在component表里用text类型的字段存储hash数据 (1)新建字段 ,这是migration的内容 class AddHintsToComponents < ActiveRecord: ...

  3. rails 中 create, new, build, save 的用法以及误区汇总

    自己很初级,初级的不能再初级,所以初次接触rails的时候,对于里面的create,new,build等方法不是很了解,用的很混乱,导致经常出现不必要的bug,很苦恼,决定,总结一下,结合网上已有资源 ...

  4. 动态嵌套form,使用Stimulus Js库(前后端不分离)

    我的git代码:https://github.com/chentianwei411/nested_form-Stimulus- Stimulus:     https://www.cnblogs.co ...

  5. rails 杂记 - erb 中的 form_helper

    原文 1. form_tag 1) 基础 Form <%= form_tag do %> Form contents <% end %> 生成 html <form ac ...

  6. Rails-Treasure chest2 嵌套表单;

    嵌套表单1-1 嵌套表单1-多 选日期时间的UI (一个jquery Plugin) 拆除前后台css和js Rich Editor, 显示输入的HTML tag 批次编辑/删除 嵌套表单1-1 核心 ...

  7. rails中accepts_nested_attributes_for应用

    Model: class Blog < ActiveRecord::Base has_many :strip_rules accepts_nested_attributes_for :strip ...

  8. Polymorphic form--多态表单

    一个ruby on rails项目,用户和公司的模型都有地址. 我要创建一个地址表,包含用户和公司表的引用,比直接做下去要好一点,这回让我的数据库设计保持干净. 我的第一印象是,这似乎很难实现,外面所 ...

  9. rails 中 create, new, build, save 的用法以及误区汇总 (转)

    自己很初级,初级的不能再初级,所以初次接触rails的时候,对于里面的create,new,build等方法不是很了解,用的很混乱,导致经常出现不必要的bug,很苦恼,决定,总结一下,结合网上已有资源 ...

随机推荐

  1. jquery.validate.js 验证表单时,在IE当中未验证就直接提交的原因

    jquery.validate.js 验证表单时,在IE当中未验证就直接提交的原因 今天利用了jquery.validate.js来验证表单,发现在火狐.谷歌浏览器当中都可以进行验证,但是在IE系列浏 ...

  2. ThinkPHP连接主从数据库

    config.php文件设置如下: return array( 'URL_MODE'=>0,   'DB_TYPE'=>'mysql',   'DB_HOST'=>'localhos ...

  3. Squid 启动/停止/重载配置文件 命令

    当你的 squid.conf 配置文档按照你的想法修改完以后,启动 squid 之旅就开始了. Squid安装设试命令: 1,初始化你在 squid.conf 里配置的 cache 目录 #/usr/ ...

  4. webservice中DateTime类型參数的传入问题

    This step-by-step article describes how to format DateTime and Date values in the XML that is extrac ...

  5. C++中没有定义类的引用。

    在有时候由于类太大.须要在类在后面定义: 比如: class Y{ void f(X); }; class X{ //一些成员数据和函数 }; //error 由于c++要求不论什么一个变量在引用之前 ...

  6. sersync简介与测试报告

    在分布式应用中会遇到一个问题,就是多个服务器间的文件如何能始终保持一致.一种经典的办法是将需要保持一致的文件存储在NFS上,这种方法虽然简单方便但却将本来多点的应用在文件存储上又变成了单点,这违背了分 ...

  7. 解决myeclipse10.x的Servers产生的at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(Unknown Source)错

    错误: java.lang.NullPointerException at com.genuitec.eclipse.ast.deploy.core.Deployment.<init>(U ...

  8. Android Studio 使用笔记:在图形界面使用 Gradle 配置项目所需jar包

    在 Android Studio 中使用第三方 jar 包,可以直接下载后添加到项目中,也可以使用 Gradle 配置进行管理.图形界面下十分简单. 点击下图中间的图标,或者选中 Model ,按F4 ...

  9. NYOJ 492 King (状态压缩)

    做题感悟:做完这题发现状态压缩有很多须要优化的地方. 解题思路:状态压缩 開始自己用的一般的思路,就和炮兵阵地,郑厂长等题类似的方法做的,開始超时,然后把数组开到了最小的极限就险过.然后看了别人的代码 ...

  10. protobuf编译安装

    为什么选择protobuf,而不选择thift和avro,原因大概几点吧,网上对比的文章很多,我主要关注以下几点 1.protobuf序列化性能最好,序列化后字节数最少. 2.protobuf是单纯的 ...