ActionController::UnknownFormat Ajax 请求在 Rails 6.0.1 中出错

ActionController::UnknownFormat error with Ajax request in Rails 6.0.1

我发现了很多类似的问题,但是 none 到目前为止已经能够解决我的错误。

在我的 recipes.show.html.haml 文件中

= simple_form_for @recipe, url: upload_photo_recipe_url, remote: true do |f|
  = f.file_field :photo, id: 'photo-uploader', class: 'd-none'
  = f.submit id: 'submit-photo', class: 'd-none'

这是在我的 show.js 文件中提交的

$('#photo-uploader').change(function() {
  $(`#edit_recipe_${recipeId}`).submit()
})

我想要的是在不刷新页面的情况下上传完成后显示这张照片,所以在recipes_controller.rb中有

def upload_photo
  @recipe = Recipe.find(params[:id])
  if @recipe.update(recipe_params)
    respond_to do |format|
      format.js
    end
  else

  end
end

预期的行为是 运行 upload_photo.js.erb 文件(目前只包含一个 console.log),但它永远不会到达那里。

照片一完成上传,我就收到一个错误页面:'ActionController::UnknownFormat in RecipesController#upload_photo',突出显示 'respond_to do |format|' 行。

我无法显示到达 upload_photo.js.erb 文件,这是怎么回事?

点击错误,我得到一个没有路由匹配[GET]“/recipes/11/upload_photo”的错误。

任何帮助将不胜感激!这几天我一直在努力解决这个问题。

感谢阅读!

问题出在我的 show.js 函数中的中间行:

$('#photo-uploader').change(function() {
  $(`#edit_recipe_${recipeId}`).submit()
})

通过将我的 show.js 更新为 'click' 表格,如

$('#photo-uploader').change(function() {
  $('#submit-photo').click()
})

照片上传后,我进入了 upload_photo.js.erb 文件。