I have a form for "Establecimientos" (Establishments), and when the user hits the submit button there's a validation that could lead to showing a modal dialog or redirecting to the "Establecimientos" index page. I've been trying with and without remote: true in the form having an error both times either with html or js calls. This is my controller (the form doesn't have remote: 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
When the else block happens the :formats=>[:js] doesn't work, the content of my create.js.erb is displayed on a new page without any rendering as plain text (the console shows it was rendered as html). Any help is appreciated
form partial
<%= 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()
})
Using remote: true makes all calls as JS but there can also be HTML calls