Rails - 使用 fields_for 创建多个嵌套对象
Rails - Use fields_for to create multiple nested objects
希望有人能帮忙解决这个问题。我有两个模型 order 和 date_order。每个订单可以有多个 date_order,我应该可以在创建订单时创建多个 date_order。
我该怎么做?如您所见,我的代码可以很好地创建一个 date_order 并将其与创建的订单相关联。
更新:我试图在我的 orders/new 文件中创建许多 "builders"。它适用于视图,并在我输入多个日期和时间时创建了一个订单。但是 fields_for 没有创建任何 date_orders.
orders_controller.rb
def new
@order = Order.new
@order.date_orders.build
end
def create
@order = Order.new(order_params)
if @order.save
flash[:success] = "blah"
redirect_to @order
else
render 'new'
end
end
private
def order_params
params.require(:order).permit(:user_id, :purpose,
date_orders_attributes: [:id, :order_date, :time_start, :time_end, :order_id])
end
order.rb
class Order < ActiveRecord::Base
has_many :date_orders, :dependent => :destroy
accepts_nested_attributes_for :date_orders, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
日期_order.rb
class DateOrder < ActiveRecord::Base
belongs_to :order
end
order/new.html.erb
<%= form_for(@order, :html => {:multipart => true}) do |f| %>
## SOME QUESTIONS ##
<%= f.fields_for :date_orders do |builder| %>
<%= builder.label :date %>
<%= builder.date_field :order_date %>
<%= builder.label :starting_time %>
<%= builder.time_field :time_start %>
<%= builder.label :ending_time %>
<%= builder.time_field :time_end %>
<% end %>
<% end %>
建造更多 orders_dates
:
class OrdersController < ApplicationController
def new
@order = Order.new
5.times { @order.date_orders.build } # < === HERE ===
end
private
def order_params
params.require(:order).permit(:user_id, :purpose,
# |- === HERE ===
date_orders_attributes: [:id, :content, :order_date, :time_start, :time_end, :order_id])
end
end
更新:
此外,将 content
添加到您的强参数白名单中。
希望有人能帮忙解决这个问题。我有两个模型 order 和 date_order。每个订单可以有多个 date_order,我应该可以在创建订单时创建多个 date_order。
我该怎么做?如您所见,我的代码可以很好地创建一个 date_order 并将其与创建的订单相关联。
更新:我试图在我的 orders/new 文件中创建许多 "builders"。它适用于视图,并在我输入多个日期和时间时创建了一个订单。但是 fields_for 没有创建任何 date_orders.
orders_controller.rb
def new
@order = Order.new
@order.date_orders.build
end
def create
@order = Order.new(order_params)
if @order.save
flash[:success] = "blah"
redirect_to @order
else
render 'new'
end
end
private
def order_params
params.require(:order).permit(:user_id, :purpose,
date_orders_attributes: [:id, :order_date, :time_start, :time_end, :order_id])
end
order.rb
class Order < ActiveRecord::Base
has_many :date_orders, :dependent => :destroy
accepts_nested_attributes_for :date_orders, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
日期_order.rb
class DateOrder < ActiveRecord::Base
belongs_to :order
end
order/new.html.erb
<%= form_for(@order, :html => {:multipart => true}) do |f| %>
## SOME QUESTIONS ##
<%= f.fields_for :date_orders do |builder| %>
<%= builder.label :date %>
<%= builder.date_field :order_date %>
<%= builder.label :starting_time %>
<%= builder.time_field :time_start %>
<%= builder.label :ending_time %>
<%= builder.time_field :time_end %>
<% end %>
<% end %>
建造更多 orders_dates
:
class OrdersController < ApplicationController
def new
@order = Order.new
5.times { @order.date_orders.build } # < === HERE ===
end
private
def order_params
params.require(:order).permit(:user_id, :purpose,
# |- === HERE ===
date_orders_attributes: [:id, :content, :order_date, :time_start, :time_end, :order_id])
end
end
更新:
此外,将 content
添加到您的强参数白名单中。