fields_for
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的更多相关文章
- rails中一个窗体多个模型——fields_for
详细参考 http://railscasts.com/episodes/73-complex-forms-part-1中part-1.2.3部分 借助field_for可以生成表单来处理两个或更多模型 ...
- 使用text存储hash类型的数据 Use text filed to store the hash map
在component表里用text类型的字段存储hash数据 (1)新建字段 ,这是migration的内容 class AddHintsToComponents < ActiveRecord: ...
- rails 中 create, new, build, save 的用法以及误区汇总
自己很初级,初级的不能再初级,所以初次接触rails的时候,对于里面的create,new,build等方法不是很了解,用的很混乱,导致经常出现不必要的bug,很苦恼,决定,总结一下,结合网上已有资源 ...
- 动态嵌套form,使用Stimulus Js库(前后端不分离)
我的git代码:https://github.com/chentianwei411/nested_form-Stimulus- Stimulus: https://www.cnblogs.co ...
- rails 杂记 - erb 中的 form_helper
原文 1. form_tag 1) 基础 Form <%= form_tag do %> Form contents <% end %> 生成 html <form ac ...
- Rails-Treasure chest2 嵌套表单;
嵌套表单1-1 嵌套表单1-多 选日期时间的UI (一个jquery Plugin) 拆除前后台css和js Rich Editor, 显示输入的HTML tag 批次编辑/删除 嵌套表单1-1 核心 ...
- rails中accepts_nested_attributes_for应用
Model: class Blog < ActiveRecord::Base has_many :strip_rules accepts_nested_attributes_for :strip ...
- Polymorphic form--多态表单
一个ruby on rails项目,用户和公司的模型都有地址. 我要创建一个地址表,包含用户和公司表的引用,比直接做下去要好一点,这回让我的数据库设计保持干净. 我的第一印象是,这似乎很难实现,外面所 ...
- rails 中 create, new, build, save 的用法以及误区汇总 (转)
自己很初级,初级的不能再初级,所以初次接触rails的时候,对于里面的create,new,build等方法不是很了解,用的很混乱,导致经常出现不必要的bug,很苦恼,决定,总结一下,结合网上已有资源 ...
随机推荐
- git merge 和 git rebase 小结(转)
git merge是用来合并两个分支的. git merge b # 将b分支合并到当前分支 同样 git rebase b,也是把 b分支合并到当前分支 ---------------------- ...
- Oracle 删除用户和表空间------创建表空间和用户
步骤一: 删除user drop user ×× cascade 说明: 删除了user,只是删除了该user下的schema objects,是不会删除相应的tablespace的. 步骤二: 删 ...
- 用python做自己主动化測试--绘制系统性能趋势图和科学计算
在性能測试中.我们常常须要画出CPU memory 或者IO的趋势图. 预计大学里.大多数人都学习过matlib, 领略了matlib绘图的强大. python提供了强大的绘图模块matplotlib ...
- SQL中拆分字符串substr及统计字符出现频数replace用法实例讲解
一.拆分字符串为若干行 例一:要求将表emp中的'king'按照每行一个单词拆成四行 注意:substr(str,pos):截取pos位置开始的字符: substr(str,pos,len):从pos ...
- CentOs上搭建nginx
目录 CentOs上搭建nginx 1. 在root环境下安装nginx 1.1 常用工具安装 1.2 关闭iptables规则 1.3 关闭SELinux 1.4 安装C/C++环境和PCRE库 1 ...
- JavaScript的split()
JavaScript split() 方法 JavaScript String 对象 定义和用法 split() 方法用于把一个字符串分割成字符串数组. 语法 stringObject.split(s ...
- sklearn函数白板
#使用make_classification构造500个样本,每个样本有20个feature from sklearn.datasets import make_classification X, y ...
- 在spring mvc中利用ajax批量删除数据
1.前台代码: $("#batchdelete").click(function(){ $.ajax({ type: "post", url: url, dat ...
- MyBatis随笔
前一阵参与了一个项目的搭建,为了快速开发再加上学一些新东西,准备采用React+Spring MVC+MyBatis的架构. 花了一些时间最终把Spring MVC+MyBatis打通. 这里总结下M ...
- Oracle关于快速缓存区应用原理
为什么oracle可以对于大量数据进行訪问时候能彰显出更加出色表现,就是通过所谓的快速缓存来实现数据的快速运算与操作.在之前的博文中我已经说过sql的运行原理,当我们訪问数据库的数据时候,首先不是从数 ...