Railscasts196,Rails 4,simple_form,JS,haml
Railscasts196, Rails 4, simple_form, JS, haml
与 Railscasts196 一样,我在 Events
中嵌套了 Attendances
,在 Event_groups
中嵌套了 Events
:
= simple_form_for(@event_group) do |f|
= f.error_notification
.form-inputs
= f.input :name
.form-actions
= f.button :submit
.h2 Events (not simple form)
= f.fields_for :events do |p|
= link_to_add_fields "Add field", p, :fields
.field
= p.label :starts_at
= p.hidden_field :_destroy
= link_to "[remove]", '#', class: "remove_fields"
%p
= p.fields_for :attendances do |f|
.field
= f.label :guest_id
= f.text_field :guest_id
= f.label :attendance_rate_id
= f.text_field :attendance_rate_id
Event_groups.js:
$(document).on 'click', 'form .remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('fieldset').hide()
event.preventDefault()
$(document).on 'click', 'form .add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
Application_helper.rb:
module ApplicationHelper
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end
end
现在我无法让 jQuery 像在 Delete
和 Add new
的 Railscasts 中那样工作。 link_to_add_fields
& remove
一定有问题。请帮忙
其实现在有更好的方法,使用cocoon gem。
它非常简单,我使用它非常成功 ;) 希望你也这样做!
我已经从 rails 演员表 196/197 兼容 rails 5 创建了一个关于嵌套模型形式的 repo。它工作得很好 nesting-models-form
与 Railscasts196 一样,我在 Events
中嵌套了 Attendances
,在 Event_groups
中嵌套了 Events
:
= simple_form_for(@event_group) do |f|
= f.error_notification
.form-inputs
= f.input :name
.form-actions
= f.button :submit
.h2 Events (not simple form)
= f.fields_for :events do |p|
= link_to_add_fields "Add field", p, :fields
.field
= p.label :starts_at
= p.hidden_field :_destroy
= link_to "[remove]", '#', class: "remove_fields"
%p
= p.fields_for :attendances do |f|
.field
= f.label :guest_id
= f.text_field :guest_id
= f.label :attendance_rate_id
= f.text_field :attendance_rate_id
Event_groups.js:
$(document).on 'click', 'form .remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('fieldset').hide()
event.preventDefault()
$(document).on 'click', 'form .add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
Application_helper.rb:
module ApplicationHelper
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end
end
现在我无法让 jQuery 像在 Delete
和 Add new
的 Railscasts 中那样工作。 link_to_add_fields
& remove
一定有问题。请帮忙
其实现在有更好的方法,使用cocoon gem。
它非常简单,我使用它非常成功 ;) 希望你也这样做!
我已经从 rails 演员表 196/197 兼容 rails 5 创建了一个关于嵌套模型形式的 repo。它工作得很好 nesting-models-form