Skip to content

Instantly share code, notes, and snippets.

@amuntasim
Last active December 18, 2015 00:29
Show Gist options
  • Save amuntasim/5697414 to your computer and use it in GitHub Desktop.
Save amuntasim/5697414 to your computer and use it in GitHub Desktop.
How to embed mercury in new action without losing ability to use select fields or any other form fields. This form is the part of a gem 'HowTo', so edit this page as per your requirements
<!-- This section is important as it redirect to editor mode and handle form submition-->
<script type="text/javascript">
<% if rich_text_enabled && params[:rich_text_enabled].nil? %>
location.href = "<%= "/editor" + request.path + "?rich_text_enabled=1"%>"
<% end %>
jQuery(window).on('mercury:ready', function () {
$('#how_to_content_submit').on('click', function () {
$.each($('[contenteditable=true]'), function () {
if ($(this).attr('target_id')) {
$('#' + $(this).attr('target_id')).val($(this).html())
}
});
Mercury.changes = false;
});
Mercury.PageEditor.prototype.save = function () {
Mercury.changes = false;
$('#how_to_content_submit').trigger('click');
}
});
</script>
<%= form_for @content, :html => {:class => 'form-horizontal'} do |f| %>
<fieldset>
<legend><%= controller.action_name.capitalize %> Content</legend>
<div class="control-group">
<%= f.label :section, :class => 'control-label' %>
<div class="controls">
<%= f.select :section_id, OPTIONS, {:prompt => 'select'}, :'chosen-enabled' => true %>
</div>
</div>
<div class="control-group">
<%= f.label :title, "Title for #{locale}", :class => 'control-label' %>
<div class="controls">
<div target_id="<%= "MERCURY_ENABLED_FIELD_ID" %>" contenteditable="true" data-mercury="full">
<%= raw f.object.send("i18n_#{locale.to_s}_title".to_sym) %>
</div>
<%= f.hidden_field "MERCURY_ENABLED_FIELD", :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :title, "Title for #{locale}", :class => 'control-label' %>
<div class="controls">
<!-- relace the form field with these following 4 lines to enable mercury-->
<div target_id="<%= "MERCURY_ENABLED_FIELD_ID" %>" contenteditable="true" data-mercury="full">
<%= raw f.object.send(MERCURY_ENABLED_FIELD.to_sym) %>
</div>
<%= f.hidden_field "MERCURY_ENABLED_FIELD", :class => 'text_area' %>
</div>
</div>
<div class="control-group">
<%= f.label :order, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :order, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary', id: 'how_to_content_submit' %>
<%= link_to 'Cancel', contents_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment