使用Carrierwave发布gif时,AJAX无法验证CSRF令牌的真实性

时间:2019-02-25 01:05:28

标签: javascript ruby-on-rails ruby ajax

当我尝试上传普通的jpeg图像和png时,我没有收到来自AJAX / Rails的任何错误。但是,当我尝试上传gif(列入白名单)时。它将用户重定向回首页。它声称在发布表单时未创建 csrf_token 。使用 create.js.erb 时如何在表单中添加csrf_token?

服务器开发日志

ImageAnnotatorSettings ias = ImageAnnotatorSettings.newBuilder()
            .setCredentialsProvider(
                    FixedCredentialsProvider.create(#InputStream of your json key#)
            )
            .build();

posts_form.html.erb

Started POST "/posts" for 127.0.0.1 at 2019-02-24 19:39:51 -0500
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "post"=>{"body_text"=>"Testing the gif again!", "photo_cache"=>""}, "commit"=>"Post"}
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 51], ["LIMIT", 1]]
Can't verify CSRF token authenticity.
   (1.0ms)  BEGIN
   (0.0ms)  COMMIT
Completed 401 Unauthorized in 110ms (ActiveRecord: 2.0ms)


Started GET "/login" for 127.0.0.1 at 2019-02-24 19:39:53 -0500
Processing by Devise::SessionsController#new as HTML
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 51], ["LIMIT", 1]]
Redirected to https://127.0.0.1/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 5ms (ActiveRecord: 1.0ms)


Started GET "/" for 127.0.0.1 at 2019-02-24 19:39:55 -0500
Processing by HomeController#index as HTML

create.js.erb

<%= simple_form_for(@post, multipart: true, remote: true) do |f| %>

  <div class="row">
    <div class="col">
      <div class="post-textarea">
          <%= f.input :body_text, as: :text, class: 'form-control post-placeholder', label: false, placeholder: 'Add a post to share with others', style: "overflow: hidden; resize: none;" %>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-md-5">
      <%= f.button :submit, 'Post', class: 'form-control submit-button bttn-gradient bttn-md bttn-royal', :data => {:disable_with => 'Wait...a min'} %>
    </div>
    <div class="col-md-5">
       <label class="bttn-minimal bttn-md bttn-royal">
        Add Photo
        <span style="display:none;">
            <%= f.input :photo, as: :file, label: false, input_html: {accept: 'image/*'} %>
            </span>
      </label>
      <% if f.object.photo? %>
        <%= image_tag f.object.photo.url(:feed_preview), class: 'img-responsive img-thumbnail' %>
      <% end %>
      <%= f.hidden_field :photo_cache %>
      <div class="post-preview-container">
        <img id="img_prev" width="100" height="100" src="#" alt="preview" class="img-thumbnail d-none"/> <br/>
        <a href="#" id="cancel_img_btn" class="d-none" onclick="return !(document.getElementById('photo').innerHTML=document.getElementById('photo').innerHTML);">Cancel
      Upload</a>
      </div>
    </div>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:1)

您可以尝试执行此操作。看看是否可行。

<%= simple_form_for(@post, multipart: true, remote: true, authenticity_token: true) do |f| %>