我想在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.像上面的例子一样呈现局部,但我不知道如何在错误累积时“停留”在模态中(我怎么能留在模态中? - &gt;我尝试过client_validation
但似乎这不能正常工作..)
2.将模态体设为加载<iframe>
的{{1}}
3. ...
哪一个最好?
答案 0 :(得分: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()