Reputation: 1
I have been trying to get around an error I don't really understand. How can there be an Invalid Authenticity Token error?
This is the error on the Terminal:
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
This is my message controller
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
private
def message_params params.require(:message).permit(:body) end
end
This is the view where I have the chat form:
<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>
Upvotes: 0
Views: 445
Reputation: 13521
Your simple_form_for
in the view might not be creating the authenticity token. Or you need to place the csrf meta tag in the head of the HTML document. Either way, you're trying to create a message the old fashioned way rather than using websockets. You have to send your messages through the client side component of actioncable as described here: http://guides.rubyonrails.org/action_cable_overview.html#client-side-components
Upvotes: 0