Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用带有remote:true的ajax的Ruby表单会出现ActionController::InvalidAuthenticationToken错误。经典的提交方式并不适用_Ruby On Rails_Ruby_Ajax_Ruby On Rails 4_Form For - Fatal编程技术网

Ruby on rails 使用带有remote:true的ajax的Ruby表单会出现ActionController::InvalidAuthenticationToken错误。经典的提交方式并不适用

Ruby on rails 使用带有remote:true的ajax的Ruby表单会出现ActionController::InvalidAuthenticationToken错误。经典的提交方式并不适用,ruby-on-rails,ruby,ajax,ruby-on-rails-4,form-for,Ruby On Rails,Ruby,Ajax,Ruby On Rails 4,Form For,我正在为一个RoR网站写一个聊天页面。我已经用HTML完成了所有的工作,我正在尝试用ajax实现它。有一个用于提交消息的表单。表单标记的内容为true do | f |%> 我的整个观点是: <!DOCTYPE html> <html> <head> <title>Battleriskopoloy - Communications</title> </head> <body&

我正在为一个RoR网站写一个聊天页面。我已经用HTML完成了所有的工作,我正在尝试用ajax实现它。有一个用于提交消息的表单。表单标记的内容为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中还有

我在网上发现了一些关于人们在从ajax切换到html提交时在rails 4中遇到相同错误的其他信息,但这似乎只是因为他们没有包含:authenticity_token=>true参数。非常感谢您的帮助。

默认情况下,Authentity\u令牌设置为false。因此,您需要在application.rb中添加这一行


我甚至不知道这个项目的来源现在在哪里。我记得虽然我确实放弃了。真的很感谢你的回答@techdreams为什么rails默认具有此属性false
config.action_view.embed_authenticity_token_in_remote_forms = true