Matestack 表单提交错误 ActionController::InvalidAuthenticityToken

问题描述

我正在制作 MateStack 文档上的 twitter 克隆。 我在提交表单时遇到错误。当我点击按钮时,它没有提交任何数据。而是打开一个错误的新页面。 这是我的控制器

class PostsController < ApplicationController
  #skip_before_action :verify_authenticity_token
  #before_action :set_post,only: [:show,:edit,:update,:destroy]
  matestack_app Twitter::App
  # GET /posts
  # GET /posts.json
  def index
    #@posts = Post.all
    render Twitter::Pages::Posts::Index
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    # respond_to do |format|
    #   if @post.save
    #     format.html { redirect_to @post,notice: 'Post was successfully created.' }
    #     format.json { render :show,status: :created,location: @post }
    #   else
    #     format.html { render :new }
    #     format.json { render json: @post.errors,status: :unprocessable_entity }
    #   end
    # end
    if @post.save
      render json: {
        message: 'Post was successfully created.'
      },status: :created
    else
      render json: {
        errors: @post.errors,message: 'Post Could not be created.'
      },status: :unprocessable_entity
    end
  end


  private
    
    # Only allow a list of trusted parameters through.
    def post_params
      params.require(:post).permit(:username,:body)
    end
end

这是我在伴侣视图中的部分形式:

    def post_form_partial
      div class: "mb-3 p-3 rounded shadow-sm" do
        heading size: 4,text: "New Post",class: "mb-3"
        form form_config_helper do
          div class: "mb-3" do
            form_input key: :username,type: :text,placeholder: "Username",class: "form-control"
          end
          div class: "mb-3" do
            form_textarea key: :body,placeholder: "What's up?",class: "form-control"
          end
          div class: "mb-3" do
            form_submit do
              button type: :submit,class: "btn btn-primary",text: "Post!"
            end
          end
        end
      end
    end

它使用以下方法在新页面上返回 ActiveRecord 错误

def handle_unverified_request
  raise ActionController::InvalidAuthenticityToken
end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)