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. js获取屏幕高度/浏览器高度

     1.window.screen.height window.screen.height:设备显示屏的高度 (1)分辨率为1080px的显示屏 (2)手机屏 2.window.screen.avail ...

  2. mkdir的参数-p的作用

    mkdir -p /nfs 也就是加上-p参数,之前只知道是递归创建目录,于是就发问了,得到的答案是: -p, --parents              no error if existing, ...

  3. 【海盗派测试分析MFQ&PPDCS】读书笔记

    使用脑图花了一张读书笔记,可能有点长

  4. Security Testing Basics

    Security Testing BasicsSoftware security testing is the process of assessing and testing a system to ...

  5. c# 获取毫秒值,时间戳

    获取时间戳(秒) (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000 获取时间戳(毫秒) (DateTime. ...

  6. Debug与Trace工具类的应用

    在写Console程序的时候,能够使用Console.WriteLine()来时时的输出程序的执行状态和各种參数此刻的信息.可是假设是Windows Form程序,我们要怎样实时的观測程序的执行状况呢 ...

  7. lua学习笔记(十三)

    math库     定义在math中     所有三角函数都使用弧度     指数和对数函数     取整函数     伪随机数math.random         调用时没有参数返回0~1之间的随 ...

  8. 使用WebStorm将项目部署到IIS

    在WebStorm中打开项目,通常WS会启动一个虚拟服务器并使用如下地址访问 但这样会有一个问题,在局域网内的其他设备,比如手机和其他电脑是不能访问这个地址的,这样就给开发和调试带来了不便.本人也是惭 ...

  9. Oracle的substr函数简单用法与substring区别

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H'  *从字符串第一个字符开始截取长度为1的字符串 subst ...

  10. 全站301跳转 PHP

    $the_host = $_SERVER['HTTP_HOST'];//取得当前域名 $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER[ ...