1
votes

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

1
If you want to respond with JavaScript, you should be using remote: true. I'm unfamiliar with the ":formats => [:js]" syntax, but it also seems counterintuitive to me. If you want JS, make a request in JS. Assuming we're going to use remote: true, can you please update your question to show both the .erb template with the submit button, and the create.js.erb response file? - Lanny Bose
As I said, the response could also be an html call, that's why I'm not using the remote: true. I used it before but then the problem is that there is going to be a JS call when I want it to be HTML. I'll add what the other files, thanks for the reply - user2782149

1 Answers

0
votes

If you want a request to be read as format.js, the easiest way is to hit the desired path plus .js at the end. As in establecimientos_path + ".js".

I believe you can also do establecimientos_path(format: "js").