我有一个Rails应用程序,我使用Bootstrap模式和Jquery将新对象添加到父对象。这现在适用于我希望添加的4个不同的子对象(我在父对象页面上呈现4个独立的模态)。我想重复使用相同的模式来呈现编辑表单(而不是新的表单),而无需在页面中添加4个额外的(大部分相同的)模式。这可能吗?使用相同的引导模式来添加一个新的对象并编辑该对象
莫代尔例如:
<!-- modal for inputting a new company -->
<div class="modal fade" id="company_modal" tabindex="-1" role="dialog" aria-labelledby="company_modal" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Add new company</h4>
</div>
<div class="modal-body">
<%= render 'companies/modal' %>
</div>
</div>
</div>
</div>
公司/莫代尔代码:
<%= form_for(@company, :html => {role: :form, 'data-model' => 'company'}, remote: true) do |f| %>
<% if @company.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@company.errors.count, "error") %> prohibited this company from being saved:</h2>
<ul>
<% @company.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :company_name, :class => "control-label" %></br>
<%= f.text_field :company_name, :class => 'form-control' %>
<span class="help-block"></span>
</div>
<div class="checkbox input">
<label>
<%= f.check_box :existing_customer %> Existing Customer?
</label>
</div>
<div id="organization_id" data-organizationid="<%= @current_user.organization.id %>">
<%= f.hidden_field :organization_id%>
</div>
<div class="actions">
<%= f.submit "Save", class: "btn btn-sm btn-success" %>
</div>
<% end %>
当前的Javascript:
$(document).ready(function() {
$('#competitor_modal').on('show');
$('.modal').on('shown.bs.modal', function() {
$("#competitor_competitor_name").focus();
var Org;
Org = $('#organization_id').attr('data-organizationid');
//pass the organization_id to the hidden text box
$('#competitor_organization_id').val(Org);
});
});
$(document).ready(function(){
$(document).bind('ajaxError', 'form#new_competitor', function(event, jqxhr, settings, exception){
// note: jqxhr.responseJSON undefined, parsing responseText instead
$(event.data).render_form_errors($.parseJSON(jqxhr.responseText));
});
});
我相信我不得不改变组织所用的JavaScript。 js以便它可以确定请求是否来自“添加新公司”按钮或者是否它来自“编辑公司”按钮。这是最佳做法还是我只是一个白痴?
谢谢。我现在想知道对我来说开发所有这些还是只使用x-editable是更有意义的。一方面,我认为Modals(用于添加和编辑)的常见用法会使用户体验更加一致,另一方面,它看起来好像已经使用该插件解决了很多这些问题。无论如何,我都很欣赏这个答案! – Zoinks10 2014-10-27 07:32:39
谢谢,是的,如果一些插件支持你想要的(并且有很好的文档记录)已经存在 - 去为它:)有时它是最好的方法(如果我们不需要一些“特殊”功能).... – nettutvikler 2014-10-27 07:38:42