码迷,mamicode.com
首页 > 其他好文 > 详细

fields_for

时间:2016-11-19 13:28:37      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:pat   build   nested   path   dir   div   save   active   password   

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

标签:pat   build   nested   path   dir   div   save   active   password   

原文地址:http://www.cnblogs.com/znsongshu/p/6080192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!