fields_for
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章
form_tag不会将动态生成的fields_for用作输入
fields_for、formtastic、ActiveMerchant 和验证错误