[英]ActionController::RoutingError (No route matches [POST] “/blogs/my-blog-post-1”) when using ActionCable channel
This is an application I started developing as part of a code-along that was produced by DevCamp While it has been great at helping me understand Rails 5 better, it is a little out of date in some places and I have had some really interesting times debugging things that have changed since its release. 这是我作为DevCamp生成的代码的一部分开始开发的应用程序,虽然它可以帮助我更好地理解Rails 5,但在某些地方有些过时了,我经历了一些非常有趣的时光调试自发布以来已更改的内容。
I am at a step where I attempt to set up a blog with comments wired through ActionCable so they update live without refreshing the page. 我正在尝试建立一个博客,其中包含通过ActionCable连接的评论,以便它们实时更新而不刷新页面。 When I post them I receive an error and, of course, the page doesn't update the way I would like.
当我发布它们时,我会收到一个错误消息,当然,该页面不会更新我想要的方式。
ActionController::RoutingError (No route matches [POST] "/blogs/my-blog-post-1")
Here is what I know I've changed to implement this. 这是我知道为实现此目的已更改的内容。 I'm fairly familiar with Ruby and Rails, but I am new to Javascript and its libraries.
我对Ruby和Rails相当熟悉,但是我是Javascript及其库的新手。 Any help you could provide would be much appreciated ( view github repo branch here )
您能提供的任何帮助将不胜感激( 在此处查看github repo分支 )
routes.rb routes.rb
Rails.application.routes.draw do
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', sign_up: 'register' }
resources :folios, except: [:show] do
put :sort, on: :collection
end
get 'folio/:id', to: 'folios#show', as: 'folio_show'
get 'about-me', to: 'pages#about'
get 'contact', to: 'pages#contact'
resources :blogs do
member do
get :toggle_status
end
end
mount ActionCable.server => '/cable'
root to: 'pages#home'
end
Blogs Channel 博客频道
class BlogsChannel < ApplicationCable::Channel
def subscribed
stream_from "blogs_#{params['blog_id']}_channel"
end
def unsubscribed
end
def send_comment(data)
current_user.comments.create!(content: data['comment'], blog_id: data['blog_id'])
end
end
connection.rb connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def guest_user
guest = GuestUser.new
guest.id = guest.object_id
guest.name = "Guest User"
guest.first_name = "Guest"
guest.last_name = "User"
guest.email = 'guest@guestuser.com'
guest = GuestUser.new
guest = GuestUser.new
guest = GuestUser.new
guest = GuestUser.new
guest
end
def connect
self.current_user = find_verified_user || guest_user
logger.add_tags 'ActionCable', current_user.email
logger.add_tags 'ActionCable', current_user.id
end
protected
def find_verified_user
if verified_user = env['warden'].user
verified_user
end
end
end
end
blogs.coffee (now in the proper file directory) blogs.coffee(现在在正确的文件目录中)
jQuery(document).on 'turbolinks:load', ->
comments = $('#comments')
if comments.length > 0
App.global_chat = App.cable.subscriptions.create {
channel: "BlogsChannel"
blog_id: comments.data('blog-id')
},
connected: ->
disconnected: ->
received: (data) ->
comments.append data['comment']
send_comment: (comment, blog_id) ->
@perform 'send_comment', comment: comment, blog_id: blog_id
$('#new_comment').on 'ajax:before', (e) ->
$this = $(this)
textarea = $this.find('#comment_content')
if $.trim(textarea.val()).length > 1
App.global_chat.send_comment textarea.val(),
comments.data('blog-id')
textarea.val('')
e.preventDefault()
return false
Compiled JS from coffeescript 从coffeescript编译JS
var comments;
comments = $('#comments');
if (comments.length > 0) {
App.global_chat = App.cable.subscriptions.create({
channel: "BlogsChannel",
blog_id: comments.data('blog-id')
}, {
connected: function() {},
disconnected: function() {},
received: function(data) {
return comments.append(data['comment']);
},
send_comment: function(comment, blog_id) {
return this.perform('send_comment', {
comment: comment,
blog_id: blog_id
});
}
});
}
$('#new_comment').on('ajax:before', function(e) {
var $this, textarea;
$this = $(this);
textarea = $this.find('#comment_content');
if ($.trim(textarea.val()).length > 1) {
App.global_chat.send_comment(textarea.val(), comments.data('blog-id'));
textarea.val('');
}
e.preventDefault();
return false;
});
It looks like you are trying to stop the form submission but you are not doing it the way it's supossed when using rails form_with
helper, it renders a remote form by default so you need to stop the rails special events. 看起来您正在尝试停止提交表单,但是您没有按照使用rails
form_with
helper时的方式进行提交,默认情况下它将呈现一个远程表单,因此您需要停止rails特殊事件。
Check the guide on how to stop a rails' remote event https://guides.rubyonrails.org/working_with_javascript_in_rails.html#stoppable-events 查看有关如何停止Rails远程事件的指南https://guides.rubyonrails.org/working_with_javascript_in_rails.html#stoppable-events
You need to listen for the ajax:before
event, not the submit
event. 您需要侦听
ajax:before
事件,而不是submit
事件。
So, change $('#new_comment').submit (e) ...
with $('#new_comment').on 'ajax:before', (e) ...
因此,将
$('#new_comment').submit (e) ...
更改$('#new_comment').submit (e) ...
$('#new_comment').on 'ajax:before', (e) ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.