Rails Ajax从控制器调用而没有远程:true

时间:2015-06-14 20:36:37

标签: javascript jquery ruby-on-rails ajax

我有一个表格,用于" Establecimientos" (企业),当用户点击提交按钮时,验证可以显示模态对话框或重定向到" Establecimientos"索引页面。我一直尝试使用和不使用远程:在使用html或js调用时都出错的形式中为true。这是我的控制器(表格没有远程: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块发生:formats => [:js] 不起作用时,我的create.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()
})

使用 remote:true 将所有调用都设为JS,但也可以进行HTML调用

1 个答案:

答案 0 :(得分:0)

如果您希望将请求读作format.js,最简单的方法是在最后点击所需的路径加.js。与establecimientos_path + ".js"中一样。

我相信你也可以做establecimientos_path(format: "js")

相关问题