是否可以根据 Rails 4 中选定的 HTML 元素以相同的形式创建多个对象?
Is it possible to create multiple objects in the same form based on selected HTML elements in Rails 4?
场景如下:
假设您有一个日历应用程序,您希望在其中向多天添加一个事件。理想情况下,您只需单击要将事件添加到的日期。然后您将填写一个表格,其中包含事件名称以及事件的开始和结束时间,然后 Rails 会自动将事件添加到所有选定的日期。
我有 3 个模型:CalendarPeriod 模型、Weekday 模型和 Event 模型。
它们的链接如下:
class CalendarPeriod < ActiveRecord::Base
has_many :weekdays
end
class Weekday < ActiveRecord::Base
belongs_to :calendar_period
has_many :events
end
class Event < ActiveRecord::Base
belongs_to :weekday
end
CalendarPeriod 具有 :start_time 和 :end_time 属性。
工作日有一个 :date 属性。
事件也有 :start_time 和 :end_time 属性。
所有的属性都是日期时间类型,外键也处理得很好。
我有一个基于 :start_time 和 :end_time 属性生成 CalendarPeriod 的表单。在创建操作中,我还为 CalendarPeriod 中的每一天生成一个工作日。所有这一切都很好。然后我有一个基于 CalendarPeriod 及其关联的工作日呈现 "calendar" 的视图。
我的问题是:
是否可以生成一个表单,让我可以根据我点击了多少个工作日同时创建多个事件?我考虑过使用 JavaScript 向我单击的每个工作日元素添加一个 class,然后使用 AJAX 呈现创建事件的表单。这可能吗?如果是这样,请分享您对我将如何实现此功能的想法。
勾选nested_form
这是一个很好的截屏视频,可以帮助您理解 screencast
这应该适用于您要实现的内容。
包括
gem "nested_form"
在你的 Gemfile 中。
如果您使用资产管道,请在 application.js
中添加以下行
//= require jquery_nested_form
给出这样的联想,
calendar_period.rb
has_many :weekdays, dependent: :destroy<br />
has_many :events, :through => :weekdays
accepts_nested_attributes_for :weekdays
accepts_nested_attributes_for :events
weekday.rb
belongs_to :calendar_period
has_many :events, dependent: :destroy
accepts_nested_attributes_for :events
event.rb
belongs_to :weekday
创建您的表单对象如下:
calendar_period_controller.rb
def new
@calendar_period = CalendarPeriod.new
@weekday = @calendar_period.weekdays.build
@event = @weekday.events.build
end
def calendar_period_params
params.require(:calendar_period).permit(:start_time, :end_time,
weekdays_attributes: [:id, :param1, :param2,
events_attributes: [:id, :start_time, :end_time]])
end
制作如下表格:
<%= nested_form_for @calendar_period do |f| %>
<%= f.text_field :start_time %>
<%= f.link_to_add "Add weekday", :weekdays %>
<%= f.fields_for :weekdays do |weekdays_form| %>
<%= weekdays_form.text_field param1 %>
<%= weekdays_form.link_to_add "Add event", :events %>
<%= weekdays_form.fields_for do |events_form| %>
<%= events_form.text_field :start_time %>
<%= events_form.link_to_remove "Remove this event" %>
<% end %>
<%= weekdays_form.link_to_remove "Remove this weekday" %>
<% end %>
<% end %>
场景如下:
假设您有一个日历应用程序,您希望在其中向多天添加一个事件。理想情况下,您只需单击要将事件添加到的日期。然后您将填写一个表格,其中包含事件名称以及事件的开始和结束时间,然后 Rails 会自动将事件添加到所有选定的日期。
我有 3 个模型:CalendarPeriod 模型、Weekday 模型和 Event 模型。 它们的链接如下:
class CalendarPeriod < ActiveRecord::Base
has_many :weekdays
end
class Weekday < ActiveRecord::Base
belongs_to :calendar_period
has_many :events
end
class Event < ActiveRecord::Base
belongs_to :weekday
end
CalendarPeriod 具有 :start_time 和 :end_time 属性。
工作日有一个 :date 属性。
事件也有 :start_time 和 :end_time 属性。
所有的属性都是日期时间类型,外键也处理得很好。
我有一个基于 :start_time 和 :end_time 属性生成 CalendarPeriod 的表单。在创建操作中,我还为 CalendarPeriod 中的每一天生成一个工作日。所有这一切都很好。然后我有一个基于 CalendarPeriod 及其关联的工作日呈现 "calendar" 的视图。
我的问题是:
是否可以生成一个表单,让我可以根据我点击了多少个工作日同时创建多个事件?我考虑过使用 JavaScript 向我单击的每个工作日元素添加一个 class,然后使用 AJAX 呈现创建事件的表单。这可能吗?如果是这样,请分享您对我将如何实现此功能的想法。
勾选nested_form
这是一个很好的截屏视频,可以帮助您理解 screencast
这应该适用于您要实现的内容。
包括
gem "nested_form"
在你的 Gemfile 中。
如果您使用资产管道,请在 application.js
中添加以下行//= require jquery_nested_form
给出这样的联想,
calendar_period.rb
has_many :weekdays, dependent: :destroy<br />
has_many :events, :through => :weekdays
accepts_nested_attributes_for :weekdays
accepts_nested_attributes_for :events
weekday.rb
belongs_to :calendar_period
has_many :events, dependent: :destroy
accepts_nested_attributes_for :events
event.rb
belongs_to :weekday
创建您的表单对象如下: calendar_period_controller.rb
def new
@calendar_period = CalendarPeriod.new
@weekday = @calendar_period.weekdays.build
@event = @weekday.events.build
end
def calendar_period_params
params.require(:calendar_period).permit(:start_time, :end_time,
weekdays_attributes: [:id, :param1, :param2,
events_attributes: [:id, :start_time, :end_time]])
end
制作如下表格:
<%= nested_form_for @calendar_period do |f| %>
<%= f.text_field :start_time %>
<%= f.link_to_add "Add weekday", :weekdays %>
<%= f.fields_for :weekdays do |weekdays_form| %>
<%= weekdays_form.text_field param1 %>
<%= weekdays_form.link_to_add "Add event", :events %>
<%= weekdays_form.fields_for do |events_form| %>
<%= events_form.text_field :start_time %>
<%= events_form.link_to_remove "Remove this event" %>
<% end %>
<%= weekdays_form.link_to_remove "Remove this weekday" %>
<% end %>
<% end %>