我一直试图解决一个我不太懂的错误。怎么会出现无效的真实性令牌错误?
这是终端上的错误:
Started GET "/serviceworker.js" for 127.0.0.1 at 2018-01-16 17:16:46 +0000
ActionController::RoutingError (No route matches [GET] "/serviceworker.js"):
actionpack (5.0.5) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.5) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.5) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.5) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.5) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.0.5) lib/rails/engine.rb:522:in `call'
puma (3.11.0) lib/puma/configuration.rb:225:in `call'
puma (3.11.0) lib/puma/server.rb:624:in `handle_request'
puma (3.11.0) lib/puma/server.rb:438:in `process_client'
puma (3.11.0) lib/puma/server.rb:302:in `block in run'
puma (3.11.0) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
Rendering /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
Rendering /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.9ms)
Rendered collection of /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/routes/_route.html.erb [42 times] (9.2ms)
Rendered collection of /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/routes/_route.html.erb [1 times] (0.4ms)
Rendered /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/routes/_table.html.erb (16.0ms)
Rendering /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.3ms)
Rendered /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (134.3ms)
Started POST "/chatrooms/1/messages" for 127.0.0.1 at 2018-01-16 17:16:52 +0000
Processing by MessagesController#create as HTML
Parameters: {"utf8"=>"✓", "message"=>{"body"=>"hi"}, "chatroom_id"=>"1"}
Can't verify CSRF token authenticity.
Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:
Started POST "/__better_errors/b0e0d47806bf58c8/variables" for 127.0.0.1 at 2018-01-16 17:16:53 +0000
这是我的消息控制器
class MessagesController < ApplicationController
def create
@message = Message.new(message_params)
@chatroom = Chatroom.find(params[:chatroom_id])
@message.chatroom = @chatroom
@message.sender = current_user
if @message.save
respond_to do |format|
format.html { redirect_to chatroom_path(@chatroom) }
format.js
end
else
respond_to do |format|
format.html { render "chatrooms/show" }
format.js
end
end
end
私有
def message_params params.require(:消息).permit(:体) 端
端
这是我有聊天表格的视图:
<div class="col-xs-4">
<div class="chat-header"><h4><%= @chatroom.session.title %></h4></div>
<div class="messages">
<% @chatroom.messages.each do |message| %>
<%= render "messages/message", message: message, user_is_messages_author: message.from?(current_user), c_user: current_user %>
<% end %>
</div>
<div id="create-message">
<%= simple_form_for [ @chatroom, Message.new ], remote: true, html: {autocomplete: "off" } do |f| %>
<%= f.input :body, label: false %>
<% end %>
</div>
</div>
答案 0 :(得分:0)
视图might not be creating the authenticity token中的simple_form_for
。或者您需要将csrf meta tag放在HTML文档的头部。无论哪种方式,您都试图以旧式方式创建消息,而不是使用websockets。您必须通过actioncable的客户端组件发送消息,如下所述:http://guides.rubyonrails.org/action_cable_overview.html#client-side-components