Javascript AJAX可以';t使用Carrierwave发布GIF时验证CSRF令牌的真实性

Javascript AJAX可以';t使用Carrierwave发布GIF时验证CSRF令牌的真实性,javascript,ruby-on-rails,ruby,ajax,Javascript,Ruby On Rails,Ruby,Ajax,当我尝试上传普通jpeg图像和PNG时,我没有收到来自AJAX/Rails的错误。但是,当我试图上传一个gif(它是白名单)时。它将用户重定向回主页。它声称在表单发布时未创建任何csrf_令牌。使用create.js.erb时,如何向表单添加csrf_令牌 服务器开发日志 Started POST "/posts" for 127.0.0.1 at 2019-02-24 19:39:51 -0500 Processing by PostsController#create as HTML P

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

服务器开发日志

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
posts\u form.html.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 %>
$("#container_posts").prepend("<%= j render partial: "posts/#{@post.post_type}", locals: {post: @post } %>");
$("#post_<%= @post.id %>").hide().fadeIn(1000);
$("#no-posts").hide().fadeOut();
$("#body_text").val("");
$("#img_prev").hide().fadeOut();
$("#cancel_img_btn").hide().fadeOut();

{:使用=>'等待…一分钟'}%>
添加照片

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 %>
$("#container_posts").prepend("<%= j render partial: "posts/#{@post.post_type}", locals: {post: @post } %>");
$("#post_<%= @post.id %>").hide().fadeIn(1000);
$("#no-posts").hide().fadeOut();
$("#body_text").val("");
$("#img_prev").hide().fadeOut();
$("#cancel_img_btn").hide().fadeOut();
$(“#集装箱#u柱”)。前置(“”);
$(“#post#”).hide().fadeIn(1000);
$(“#无帖子”).hide().fadeOut();
$(“#正文”)val(“”);
$(“img#u prev”).hide().fadeOut();
$(“#取消_img_btn”).hide().fadeOut();

您可以尝试这样做。看看这是否有效

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

您可以尝试这样做。看看这是否有效

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


PostsController处理#创建为HTML
为什么请求不是以AJAX的形式发送的,而是以普通的方式发送的?为什么在remote:true处于活动状态时不使用JS?我实际上在ngrok中工作。我将在没有它的情况下重试无论我做什么,它都会尝试创建为HTML,然后显示“无法验证CSRF令牌的真实性”完成401I我同意。但是,您的答案是正确的,因为PostsController手动包含CSRF令牌
处理#创建为HTML
为什么请求不是作为AJAX发送的,而是正常的?为什么在remote:true处于活动状态时不使用JS?我实际上在ngrok内工作。我将在没有它的情况下重试无论我做什么,它都会尝试创建为HTML,然后显示“无法验证CSRF令牌的真实性”完成401I我同意。但是,您的答案是正确的,因为手动包含了CSRF令牌。它与rails ujs而不是jquery_ujs一起工作。可能是过时的,DIT与rails ujs一起工作,而不是jquery_ujs。可能已经过时了