Rails 5.2 TinyMCE Image Uploads with Amazon S3



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

到目前为止,我已经实现了教程中的所有代码,一切都很完美,但当我尝试上传图像时,我会收到一条"收到来自服务器的错误响应"的错误消息。

在我的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

最新更新