gpt4 book ai didi

ruby-on-rails - 使用带有 remote: true 的 ajax 的 Ruby 表单会给出 ActionController::InvalidAuthenticityToken 错误。经典提交不

转载 作者:数据小太阳 更新时间:2023-10-29 06:59:03 25 4
gpt4 key购买 nike

我正在为 RoR 站点编写聊天页面。我已经在 HTML 中完成了所有工作,并且我正在尝试使用 ajax 来实现它。有一个用于提交消息的表格。表单标签显示为 <%= 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>

Controller :

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

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

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

最佳答案

默认情况下 authenticity_token 设置为 false。所以你需要在你的 application.rb

中添加这一行
config.action_view.embed_authenticity_token_in_remote_forms = true

关于ruby-on-rails - 使用带有 remote: true 的 ajax 的 Ruby 表单会给出 ActionController::InvalidAuthenticityToken 错误。经典提交不,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24319353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com