Rails中 accepts_nested_attributes_for 的用法

accepts_nested_attributes_for 在rails中,可以让嵌套表单变得很简单。

举个例子:

class Product < ActiveRecord::Base
  has_one :detail
end

class Detail < ActiveRecord::Base
  belongs_to :product
end

<% form_for :product do |f| %>
  <%= f.text_field :title %>
  <% fields_for :detail do |detail| %>
    <%= detail.text_field :manufacturer %>
  <% end %>
<% end %>

class ProductsController < ApplicationController
  def create
    @product = Product.new(params[:product])
    @detail = Detail.new(params[:detail])

    Product.transaction do
      @product.save!
      @detail.product = @product
      @detail.save
    end
  end
end
Product和Detail模型是一对多的关联, 我们想创建一个产品同事创建一个新的制造商,在这个例子中, 我们创建一个product对象和一个detail对象, 然后通过事务关联他们,但是为什么我们在控制器中做这样复杂的创建,产品模型应该负责创建产品的详情。

重构:

class Product < ActiveRecord::Base
  has_one :detail
  accepts_nested_attributes_for :detail
end

<% form_for :product do |f| %>
  <%= f.text_field :title %>
  <% f.fields_for :detail do |detail| %>
    <%= detail.text_field :manufacturer %>
  <% end %>
<% end %>

class ProductsController < ApplicationController
  def create
    @product = Product.new(params[:product])
    @product.save
  end
end

我们在产品模型中添加了accepts_nested_attributes_for定义, 这样产品模型就能负责产品详情模型对象的创建了, 这样是不是很酷呢
同时我们也可以在一对多的模型关系中通过 accepts_nested_attributes_for来简化关联对象的创建

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks
end

class Task < ActiveRecord::Base
  belongs_to :project
end

<% form_for @project do |f| %>
  <%= f.text_field :name %>
  <% f.fields_for :tasks do |tasks_form| %>
    <%= tasks_form.text_field :name %>
  <% end %>
<% end %>

感谢rails提供了这样神奇的方法!~

参考资料: http://www.niancode.com/?p=268
其他资料:

http://thirddirective.com/posts/8-rails-3-and-accepts_nested_attributes_for-my-mistakes

http://apidock.com/rails/v3.0.9/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for

https://rails.lighthouseapp.com/projects/8994/tickets/6227-rails3-accepts_nested_attributes_for-reject_if-is-evaluated-but-ignored

此外:

class Blog < ActiveRecord::Base
  has_many :posts
  has_one :author
  accepts_nested_attributes_for :author, :reject_if => proc { |attributes| attributes.all? {|k,v| v.blank?} }
  accepts_nested_attributes_for :posts, :reject_if => proc { |attributes| attributes.any? {|k,v| v.blank?} }
end

在新的ActiveRecord中支持使用:all_blank:any_blank 来代替上述代码:

class Blog < ActiveRecord::Base
  has_many :posts
  has_one :author
  accepts_nested_attributes_for :author, :reject_if => :all_blank
  accepts_nested_attributes_for :posts, :reject_if => :any_blank 
end

https://rails.lighthouseapp.com/projects/8994/tickets/2501-any_blank-and-all_blank-options-for-accepts_nested_attributes_for-reject_if


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值