Skip to content

Instantly share code, notes, and snippets.

@ismarsantos
Last active January 8, 2018 16:54
Show Gist options
  • Save ismarsantos/19fa7b32477b45ddceb072f17b730646 to your computer and use it in GitHub Desktop.
Save ismarsantos/19fa7b32477b45ddceb072f17b730646 to your computer and use it in GitHub Desktop.
Fields
<fieldset>
<div class="form-inline" style="margin-bottom:10px;">
<div class="form-group">
<%= f.text_field "synonyms[value]", class: "form-control" %>
</div>
<div class="form-group">
<%= f.text_field "synonyms[synonyms]", class: "form-control", style: 'width: 300px;' %>
</div>
<%= f.hidden_field :destroy %>
<button class="btn btn-default remove_fields">X</button>
</div>
</fieldset>
<%= form_for @entity, url: entity_path, method: type_metod do |f| %>
<%= f.fields_for :synonyms do |fields|%>
<% @entity.synonyms.each do |key| %>
<%= render partial: 'fields', locals: {f: fields} %>
<% end %>
<%= link_to_add_fields "Adicionar", f, :fields %>
<% end %>
<% end %>
$(document).on('click', 'form .remove_fields', function (event) {
$(this).prev('input[type=hidden]').val('1');
$(this).closest('fieldset').hide();
return event.preventDefault();
});
$(document).on('click', 'form .add_fields', function (event) {
var regexp, time;
time = new Date().getTime();
regexp = new RegExp($(this).data('id'), 'g');
$(this).before($(this).data('fields').replace(regexp, time));
return event.preventDefault();
});
class Entity
include ActiveModel::Validations
include ActiveModel::Serialization
include ActiveModel::Conversion
include ActiveModel::Model
extend ActiveModel::Naming
attr_accessor :id, :name, :synonyms, :description
...
@ismarsantos
Copy link
Author

Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXX==", "entity"=>{"name"=>"", "description"=>"", "synonyms"=>{"synonyms"=>{"value"=>"v1", "synonyms"=>"sinonimo1, sinonimo2"}, "destroy"=>""}}, "commit"=>"Salvar"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment