1

我想在new页面中包含一个模式,该模式可以“动态”创建另一个模型的条目,并使用新条目刷新选择而不刷新页面。

我做的第一次尝试是这样的:

提升模态的页面(好的,它正确地提升了其他模型的形式)

#new.html.erb
<%= simple_form_for(@odl) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
<%= f.collection_select :client_id, Client.order("LOWER(last_name) ASC"), :id, :last_name_and_name_and_company, :prompt => "Select a client" %>
      <a href="#client_modal" role="button" data-toggle="modal">Create new client</a>

#...some code...
#...and the modal that raise correctly

<div id="client_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="client_modal_label" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="client_modal_label">Create a new client</h3>
  </div>
  <div class="modal-body">
    <% @client = Client.new %>
    <%= render :partial => "clients/form" %>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Create Client</button>
  </div>
</div>

我希望如果新客户端表单中的某些输入是错误的,它会在模式中即时显示,而不是在新的“经典”页面中。

Client.new 控制器如下:

#clients_controller

def create 
#create new client
 if save
   redirect_to to_a_path, :notice => "Client created successfull!"      
 else
      flash[:error] = "Error!"
      render :action => "new"
 end
end

我认为可以遵循的解决方案:
1.像上面的例子一样渲染一个部分,但是我不知道当错误发生时如何“停留”在模态中(我怎样才能留在模态中?->我试过了client_validation但似乎不能正常工作..) 2.
将模态体设置为<iframe>加载new_client_path
3 ..

哪一个是最好的?

4

1 回答 1

1

经过一些技巧后,我找到了一个可行的解决方案:

模态控制器的视图:

<div id="client_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="client_modal_label" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="client_modal_label">Create new client</h3>
  </div>
  <div class="modal-body">
    <%= simple_form_for Client.new, html: {"data-type" => :json}, remote: true do |f| %>
  <div id="error_explanation" style="display:none;">
  </div>

  <div class="form-inputs">
    # the inputs
    # ... and "closing" the modal

client_controller.create: _

def create
 # save the data
 if @client.save
    respond_to do |format|
          format.html { redirect_to anagrafe_index_path, :notice => "Client create!"}
          format.json { render json: @client, status: :created, location: @client }
    end
 else
    respond_to do |format| 
        format.html { render :action => 'new'}
        format.json { render json: @client.errors.full_messages, status: :unprocessable_entity }
    end
 end
end

引发模态的 js.coffee :

$ ()->
  $("form.new_client").on "ajax:success", (event, data, status, xhr) ->
    $("form.new_client")[0].reset()
    $('#client_modal').modal('hide')
    # refreshing the select
    $('#error_explanation').hide()

  $("form.new_client").on "ajax:error", (event, xhr, status, error) ->
    errors = jQuery.parseJSON(xhr.responseText)
    errorcount = errors.length
    $('#error_explanation').empty()
    if errorcount > 1
      $('#error_explanation').append('<div class="alert alert-error">The form has ' + errorcount + ' errors.</div>')
    else
      $('#error_explanation').append('<div class="alert alert-error">The form has 1 error.</div>')
    $('#error_explanation').append('<ul>')
    for e in errors
      $('#error_explanation').append('<li>' + e + '</li>')
    $('#error_explanation').append('</ul>')
    $('#error_explanation').show()
于 2013-06-21T13:01:16.980 回答