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,很苦恼,决定,总结一下,结合网上已有资源 ...
随机推荐
- PDF如何自动滚动阅读
PDF如何自动滚动阅读 视图---页面显示---自动滚动 快捷键 Ctrl +shift+H
- vue-router 重定向
1.从 /a 重定向到 /b const router = new VueRouter({ routes: [ { path: '/a', redirect: '/b' } ] }) 重定向的目标也可 ...
- 两个页面相同js方法兼容
1. a.js页面 //Js获取Url参数 function request(paras) { var url = location.href; var paraString = url.substr ...
- asp.net core mvc视频A:笔记3-4.母版页与部分视图
新建项目3.4, 新建一个共享文件,一般存放在Shared目录下方 选择 如果安装了Reshaper插件可以这样添加(插件在本人博客中找) 代码 创建一个空的控制器TestController 使用布 ...
- 防火墙firewall的设置和查看
systemctl start firewalld.service # 开启防火墙firewallsystemctl stop firewalld.service # 停止防火墙firewall sy ...
- Redis 的学习和使用
安装Redis 官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 windows版:https://github.com ...
- Atitit. Object-c语言 的新的特性 attilax总结
Atitit. Object-c语言 的新的特性 attilax总结 1.1. Object-C语言由 Brad J.Cox于20世纪80年代早期设计,1 1.2. Object-C新增的数据结构: ...
- iOS中三种方式实现登录界面播放视频或gif效果
现在app都做的越来越炫酷,各种动画效果,各种特效很好的提高了用户的体验.很多app在登录界面都使用了动画效果,比如Uber,Keep,QQ等等.这些动画效果基本都是使用gif或者MP4来实现的. 效 ...
- rm 命令简要
rm 单独使用只能删除文件不能删除文件夹 rm -r 可以删除文件夹和文件 1.rm test.txt 删除文件 2.rm -r test.txt 每次删除的时候都询问是 ...
- cobbler+koan
cobbler+koan自动重装客户机 koan是kickstart-over-a-network的缩写,它是cobbler的客户端帮助程序,koan允许你通过网络提供虚拟机,也允许你重装已经存在 ...