V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ChrisZou
V2EX  ›  Ruby on Rails

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

  •  
  •   ChrisZou · 2020-11-19 22:19:39 +08:00 · 3615 次点击
    这是一个创建于 1225 天前的主题,其中的信息可能已经有所发展或是发生改变。

    正文见 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 22:33:03 +08:00
    看的一脸懵,api 的情况下,是不需要 csrf token 的,因为没有意义。
    所以应该跳过 csrf
    方法也很简单。在 application_controller.rb 里面,修改 protect_from_forgery
    把 protect_from_forgery with: :exception 修改成 protect_from_forgery with: :null_session
    实际上,文件里有注释。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1010 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 19:26 · PVG 03:26 · LAX 12:26 · JFK 15:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.