将ajax与remote:true一起使用的Ruby表单会给出ActionController :: InvalidAuthenticityToken错误.经典提交不 [英] Ruby form using ajax with remote: true gives ActionController::InvalidAuthenticityToken error. Classic submission does not

查看:93
本文介绍了将ajax与remote:true一起使用的Ruby表单会给出ActionController :: InvalidAuthenticityToken错误.经典提交不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为RoR网站编写聊天页面.我已经用HTML解决了所有问题,并且尝试使用Ajax实现它.有一种用于提交消息的表格.表单标签读取<%= form_for(@chat, remote: true, :authenticity_token => true) do |f| %>

I am writing a chat page for a RoR site. I have it all worked out in HTML and I am trying to implement it with ajax. There is a form for submission of messages. the form tag reads <%= form_for(@chat, remote: true, :authenticity_token => true) do |f| %>

我的整个观点:

  <!DOCTYPE html>
<html>
    <head>
        <title>Battleriskopoloy - Communications</title>
    </head>
    <body>
        <br>
        <div id="heading_1" align="center">
            <br><span id="title"><strong>[ . . . Communications . . . ]</strong></span>
        </div><br>
        <div id="sidebar"><br>
            <% $chat_users.each do |user| %>
            <% user = User.find_by_username(user) %>
            <%= button_to user.username, flop_chat_path(user), :class => "select", :method => :get  %>
            <% end %>
        </div>
        <div id="display">
            <% $messeges.each do |i| %>
                <% if i[1] == "from" %>
                <p style="color:#000000; text-align:right; margin-right:5%;font-family:courier"><%= i[0] %></p>
                <br>
                <% else %>
                <p style="color:#FF0000; text-align:left; margin-left:5%; font-family:courier"><%= i[0] %></p>
                <br>
                <%end%>
            <%end%>
        </div>
        <div id="textfield">
            <%= form_for(@chat, remote: true, :authenticity_token => true) do |f| %>
            <%= f.text_field :content, id: "compose" %>
            <br>
            <br>
            <%= f.submit "Send Messege", id: "submit" %>
        <% end %>
        </div>
    </body>
</html>

控制器:

class ChatsController < ApplicationController
     $messeges = Array.new(10, " ")
def new
    $messeges = Array.new
    if $ruser != nil
    $messeges = Array.new
    Chat.all.each_with_index do |i, index|
        if i.recipient == current_user.id && i.sender == $ruser.id
            $messeges.push([i.content, "to"])
        end
        if i.recipient == $ruser.id && i.sender == current_user.id
            $messeges.push([i.content, "from"])
        end
    end
    end
    $chat_users = Array.new
    User.all.each do |user|
        if user != nil && current_user != nil
        if user.game_id == current_user.game_id && user.id != current_user.id
            $chat_users.push(user.username)
        end
        end
    end
    @chat = Chat.new
end

def create
    if $ruser != nil
    @chat = Chat.new(chat_params)
    @chat.recipient = $ruser.id
    @chat.sender = current_user.id
    @chat.save
    end
    redirect_to "/comms/new"
end

def flop
    $ruser = User.find(params[:id])
    $messeges = Array.new
    Chat.all.each_with_index do |i, index|
        if i.recipient == current_user.id && i.sender == $ruser.id
            $messeges.push([i.content, "to"])
        end
        if i.recipient == $ruser.id && i.sender == current_user.id
            $messeges.push([i.content, "from"])
        end
    end
    redirect_to "/comms/new"
end

private
    def chat_params
        params.require(:chat).permit(:content)
    end
end

此外,我的application.html.erb中有<%= csrf_meta_tags %>

Additionally, I have <%= csrf_meta_tags %> in my application.html.erb

我在网上发现了一些其他信息,人们在从ajax切换到html提交时遇到了Rails 4的相同错误,但这似乎仅仅是因为他们没有包含:authenticity_token => true参数.任何帮助,我们将不胜感激.

I had found some other things online about people getting the same error with rails 4 when switching from ajax to html submission but that seemed only to be because they didn't have the :authenticity_token => true parameter included. Any help is greatly appreciated.

推荐答案

默认情况下,authenticity_token设置为false.因此,您需要在application.rb

By default authenticity_token is set to false. So you need to add this line in your application.rb

config.action_view.embed_authenticity_token_in_remote_forms = true

这篇关于将ajax与remote:true一起使用的Ruby表单会给出ActionController :: InvalidAuthenticityToken错误.经典提交不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆