我有一个“Establecimientos”(企业)的表单,并且当用户点击提交按钮有一个验证,可能会导致显示模式对话框或重定向到“Establecimientos”索引页面。我一直在尝试使用和不使用遥控器:在使用html或js调用两次都出现错误的情况下为true。这是我的控制器(形式不具有远程:真):从控制器没有远程的轨道Ajax调用:true
def create
@establecimiento = Establecimiento.new(establecimiento_params)
if !Establecimiento.exists?(:nit => @establecimiento.nit)
respond_to do |format|
if @establecimiento.save
format.html { redirect_to establecimientos_url, notice: 'El establecimiento se creó exitosamente.'.html_safe }
format.json { render :show, status: :created, location: @establecimiento }
else
format.html { render :new }
format.json { render json: @establecimiento.errors, status: :unprocessable_entity }
end
end
else
respond_to do |format|
format.js { render :action => 'create'}
format.html { render :action => 'create', :formats=>[:js]}
end
end
end
当else块发生在:格式=> [:JS]不起作用,我的创建的内容。 js.erb显示在新页面上,没有任何呈现为纯文本(控制台显示它呈现为html)。任何帮助表示赞赏
形式部分
<%= bootstrap_form_for(@establecimiento, label_errors: true, layout: :horizontal, label_col: "col-sm-3", control_col: "col-sm-7") do |f| %>
...content
<%= f.form_group do %>
<%= f.submit %>
<% end %>
<%= render 'duplicated_modal' %>
<% end %>
create.js.erb
<%@duplicated_establishment = Establecimiento.find_by_nit(@establecimiento.nit) %>
$('.modal-body').html("Ya existe un establecimiento con el mismo NIT (<%= j @duplicated_establishment.nit.to_s %>) ubicado en <%= j @duplicated_establishment.direccion_establecimiento %>, desea crear una nueva sede?");
$('.modal-footer').html("<%= j button_to 'Crear nueva sede', new_location_path(@establecimiento), class: "btn btn-primary", "method"=>"post", remote: true%> <button type='button' class='btn btn-default' data-dismiss='modal'>Cancelar</button> ")
$('#duplicated_conf_modal').modal("show");
$('#duplicated_conf_modal').on('shown.bs.modal', function() {
$('.first_input').focus()
})
使用远程:真使所有来电作为JS但也可以是HTML来电
如果你想用JavaScript做出回应,你应该使用remote:true。我不熟悉“:formats => [:js]”语法,但它对我来说似乎也是违反直觉的。如果你想要JS,请在JS中提出请求。假设我们要使用remote:true,那么您能否更新您的问题以显示带提交按钮的.erb模板和create.js.erb响应文件? –
正如我所说的,响应也可能是一个html调用,这就是为什么我没有使用remote:true。我之前使用过它,但问题是当我希望它成为HTML时,将会有JS调用。我将添加其他文件,谢谢你的回复 – user2782149