ActionController::UnknownFormat on AJAX with remote true

Posted

技术标签:

【中文标题】ActionController::UnknownFormat on AJAX with remote true【英文标题】: 【发布时间】:2018-04-27 06:46:34 【问题描述】:

我正在尝试通过远程 true 调用打开 Bootstrap 模态,因为我需要将一些参数传递给模态。

所以,我有这个代码:

index.html

...

<%= link_to("<i class='fa fa-upload'></i>".html_safe, :controller => :blocked_phone_numbers, :action => :show_modal, :blocked_phone_numbers_group_id => blocked_phone_numbers_groups.id, :class => "btn btn-primary btn-xs", remote: true) %>

...

<div id="modalImportNumbers">
  <%= render partial: 'show_modal' %>
</div>

...

blocked_phone_numbers_controller.rb

def show_modal   @blocked_phone_numbers_group = BlockedPhoneNumbersGroup.find(params[:blocked_phone_numbers_group_id]) respond_to do |format|
    format.js   end end

show_modal.js.erb

$('#modalImportNumbers').html("<%= j render partial: 'show_modal' %>");
$('#modalImportar').modal('show');

_show_modal.html.erb

<div id="modalImportar" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header" style="background-color: #5a4371; color: #fff">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="modalLabel"><%= t(:item_7) %></h4>
      </div>
      <div class="row">
        <div class="col-xs-12 col-md-12 col-md-offset-0">
          <div class="modal-body">

            <%= form_tag(:action => "import", :multipart => true) do |form| %>

              ...

            <% end %>

          </div>
        </div>
      </div>
    </div>
  </div>
</div>

routes.rb

Rails.application.routes.draw do

  root 'login#index'

  match ':controller(/:action(/:id))(.:format)', via: [:get, :post]
end

问题是我不断收到此错误:

ActionController::UnknownFormat (ActionController::UnknownFormat):

app/controllers/blocked_phone_numbers_controller.rb:25:in `show_modal'

NameError: 未初始化的常量 Mime::HTML

我的 application.js 上有 jQuery 导入,所以,我在这里阅读了很多线程,但我不知道发生了什么。

有人可以帮我理解这个错误吗?

我正在使用 Rails 5.1 和 Bootstrap 3.7

谢谢!

【问题讨论】:

让我知道以获得进一步的指导。 【参考方案1】:

routes.rb

match '/show_modal' => 'blocked_phone_numbers#show_modal', as: 'show_modal', via: [:get, :post]

index.html.erb

...

<%= link_to show_modal_path(blocked_phone_numbers_group_id: blocked_phone_numbers_groups.id), class: 'btn btn-primary btn-xs', method: :post, :remote => true do%>
  <i class='fa fa-upload'></i%>
<%end%> 

...

<div id="modalImportNumbers">
  <%= render partial: 'show_modal' %>
</div>

...

【讨论】:

以上是关于ActionController::UnknownFormat on AJAX with remote true的主要内容,如果未能解决你的问题,请参考以下文章