Rails 5.2 使用 Amazon S3 上传 TinyMCE 图像

Rails 5.2 TinyMCE Image Uploads with Amazon S3

我正在使用 this 教程尝试在我的 rails 5.2 应用程序上使用 TinyMCE 所见即所得编辑器来促进图像上传。

到目前为止,我已经实现了教程中的所有代码并且一切正常,但是当我尝试上传图像时,我收到 "Got a bad response from the server" 错误消息。

在我的 heroku logs 中,我得到了这个:

FATAL -- : [527c4468-9ebe-475a-97a9-380bfc327aab] ActionController::RoutingError (uninitialized constant #<Class:0x000055b6d991e020>::EditController
2020-02-22T17:34:07.814727+00:00 app[web.1]: Did you mean?  DeviseController):

这是我正在使用的路线:

post '/tinymce_assets', to: 'article/edit#image_upload'

使用这些控制器方法:

  def image_upload
    file = params[:file]
    url = upload_file(file)
      render json: {
        image: {
          url: url
        }
      }, content_type: "text/html"
  end


  private

    def upload_file(file)
      s3 = Aws::S3::Resource.new(region:ENV['AWS_REGION'])
      obj = s3.bucket(ENV['S3_BUCKET_NAME']).object('articles/images/content/' + filename(file))
      obj.upload_file(file.tempfile, {acl: 'public-read'})
      obj.public_url.to_s
    end

    def filename(file)
      file.original_filename.gsub(/[^a-zA-Z0-9_\.]/, '_')
    end

这是初始化它:

<%= f.text_area :body, class: "tinymce", rows: 20, cols: 120 %>
<%= tinymce :content_css => asset_path('application.css')%>

...


<script>
  $(document).ready(function() {
    tinymce.init({
      selector: "textarea.tinymce",  // change this value according to your HTML
    });
  });
</script>

谁能看出我哪里出错了?

Rails 正在尝试查找 EditController 因为您的路线:post '/tinymce_assets', to: 'article/edit#image_upload'

这条路线表明它应该查看的控制器是 Article::EditController。由于您实际上是在寻找 ArticlesController,因此您应该将路线更改为:

post '/tinymce_assets', to: 'articles#image_upload'

Rails 路由文档可能会有帮助,特别是第 2.2 和 2.6 节。 https://guides.rubyonrails.org/routing.html