Last active
January 8, 2018 16:54
-
-
Save ismarsantos/19fa7b32477b45ddceb072f17b730646 to your computer and use it in GitHub Desktop.
Fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Entity | |
include ActiveModel::Validations | |
include ActiveModel::Serialization | |
include ActiveModel::Conversion | |
include ActiveModel::Model | |
extend ActiveModel::Naming | |
attr_accessor :id, :name, :synonyms, :description | |
... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXX==", "entity"=>{"name"=>"", "description"=>"", "synonyms"=>{"synonyms"=>{"value"=>"v1", "synonyms"=>"sinonimo1, sinonimo2"}, "destroy"=>""}}, "commit"=>"Salvar"}