【问题标题】:Rails Remote True Ajax ActionController::UnknownFormatRails Remote True Ajax ActionController::UnknownFormat
【发布时间】:2020-05-22 03:48:31
【问题描述】:

我的模型中有一个小型更新方法,它只更新一个字段,设置在图表中(所以我看到每一行的表格)。它工作正常,但希望它使用 AJAX 进行更新,以便能够快速进行多次更新,而无需等待整个页面重新加载。

单元格如下所示:

      <td style="min-width: 150px" id="goody_cell<%= blog.id %>">
        <%= render partial: "admin/goody_cell", locals: { blog: blog } %>
      </td>

以此为部分:

<% if blog.variation_id %>
  <%= Variation.find(blog.variation_id).name %>
<% end %>

<%= simple_form_for(blog, url: update_goody_path(blog), remote: true, method: :patch, authenticity_token: true) do |f| %>
  <% variation_collection = [] %>
  <% Variation.all.each do |lm| %>
    <% variation_collection << ["#{lm.name}", "#{lm.id}"] %>
  <% end %>
  <div class="row text-left">
    <div class="form-group col-12 mb-0 pb-0">
      <%= f.input :variation_id, label: false, prompt: "Choose a Related Goody", input_html: { id: "lmFor" + blog.id.to_s, class: 'browser-default', onchange: "this.form.submit();" }, collection: variation_collection, required: false %>
    </div>
  </div> <!-- row -->
<% end %>

我的blogs/update_goody.js.erb 看起来像这样:

$('#goody_cell<%= blog.id %>').html('<%= j render partial: "admin/goody_cell", locals: { blog: @blog } %>');

而我的控制器方法是这样的:

  def update_goody
    @blog = Blog.friendly.find(params[:id])
    if @blog.update!(blog_params)
      respond_to do |format|
        format.js
      end
    else
      format.html { render :edit }
      format.json { render json: @blog.errors, status: :unprocessable_entity }
    end
  end

我有这样的路线:

patch "blogs/:id/update_goody" => "blogs#update_goody", as: "update_goody"

当我尝试更新它时,它确实会更新值,但在渲染部分之前,我收到一条错误消息:

ActionController::UnknownFormat - ActionController::UnknownFormat:
  app/controllers/blogs_controller.rb:178:in `update_goody'

我看过像 this 这样的 SO 帖子,但他们都说通过添加 remote: true 修复了错误,我已经有了。

谁能帮我搞定这个工作?

【问题讨论】:

  • 难道 update_goody.js 应该是 update_goody.js.erb 吗?
  • @roshiend 是的,它完全应该。不幸的是,这并没有改变我得到的错误。
  • 你的日志说了什么?尝试在 format.js 之前添加 format.html
  • 看起来您在视图博客中缺少 update_goody .html.erb 文件

标签: ruby-on-rails ajax


【解决方案1】:

确保您的views/blogs 文件夹中同时拥有update_goody.html.erbupdate_goody.js.erb

您不必向update_goody.html.erb 添加任何内容,因为它根本不会呈现。 在你的控制器中

def update_goody
    @blog = Blog.friendly.find(params[:id])
    if @blog.update!(blog_params)
      respond_to do |format|
        format.html
        format.js
      end
    else
      format.html { render :edit }
      format.json { render json: @blog.errors, status: :unprocessable_entity }
    end
  end

注意format.htmlrespond_to 块中。不管你是否添加它,因为你在表单中设置了remote: true

参考this

【讨论】:

  • 这没有抛出错误,但它把我重定向到空白的 HTML 页面。
  • 不。 respond_to do |format| format format.js do @blog = Blog.friendly.find(params[:id]) end 给了我同样的ActionController::UnknownFormat 错误。
  • 我以前没有使用过简单的形式,但我认为你的远程真不在表格中。从括号中取出远程真并将它们像&lt;%=simple_form_for(blog, url: update_goody_path(blog), method: :patch, authenticity_token: true), remote: true do%&gt;this
  • 您建议的语法会产生不可接受的格式错误。我尝试完全不带括号 &lt;%= simple_form_for blog, url: update_goody_path(blog), method: :patch, authenticity_token: true, remote: true do |f| %&gt; 但这只是有相同的 ActionController::UnknownFormat 错误。
  • &lt;%=simple_form_for @blog, :url =&gt; {:controller =&gt; 'blogs', :action =&gt; 'update_goody', :method =&gt; patch,:authenticity_token =&gt; true,:remote =&gt; true } do |f|%&gt;
【解决方案2】:

使用local: false(Rails 6)代替remote: true

【讨论】:

    猜你喜欢
    • 2018-04-27
    • 2015-09-14
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多