ChrisZou
V2EX  ›  Ruby on Rails

使用 Vue 前端、Rails 后端实现图片上传的功能

By ChrisZou at 2020 年 11 月 19 日 · 9844 次点击

正文见 https://chriszou.com/2020/11/16/upload-image-using-vue-and-rails/
核心方法是使用 formData 作为 ajax 的 body,之前主要不清楚的点在于 csrf token 的传递。
当然,这里的方法可能不是最好的。如果哪位大神有更好的方法实现,还请不吝指教!

1 条回复  •  2020-11-25 22:33:03 +08:00
wangkun025
   1
wangkun025  
   2020 年 11 月 25 日
看的一脸懵,api 的情况下,是不需要 csrf token 的,因为没有意义。
所以应该跳过 csrf
方法也很简单。在 application_controller.rb 里面,修改 protect_from_forgery
把 protect_from_forgery with: :exception 修改成 protect_from_forgery with: :null_session
实际上,文件里有注释。
© 2026 V2EX · 20ms · 3.9.8.5