ruby-on-rails - 如何修复站点控制台中的“Rails.ajax”错误?
问题描述
我有 2 种不同的帖子类型(新闻和文章)。当我去新闻并尝试评论任何新闻时,什么都没有发生。在谷歌浏览器控制台中它说:
POST /news/artifact-v-spiske-liderov-prodazh-steam-po-itogam-2018-goda/comments/ 404
rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:7
Rails.ajax @ rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:7
Rails.handleRemote @ rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:31
(anonymous) @ rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:5
但!当我尝试在文章类型中发表评论时,一切都很好,评论工作。什么是问题?
我的 _form.html.haml:
- if user_signed_in? && current_user.mute_to && current_user.mute_to > Time.current
%div
%strong
= "Вы не можете писать комментарии до #{current_user.mute_to.strftime("%F %R")}"
- elsif user_signed_in?
= form_with model: [commentable, Comment.new], html: { class:
local_assigns[:class], data: { target: local_assigns[:target] } } do |form|
.form-group
= form.text_area :body, placeholder: "Напишите комментарий (минимум 3 символа)", class: "form-control"
.form-group
= form.hidden_field :parent_id, value: local_assigns[:parent_id]
= form.submit 'Отправить', class: "btn"
我的 _comment.html.haml:
- nesting = local_assigns.fetch(:nesting, 1)
- max_nesting = local_assigns[:max_nesting]
- continue_thread = local_assigns[:continue_thread]
= tag.div id: dom_id(comment), class: "border-left pl-4 my-4" do
- if comment.deleted?
%strong [комментарий удален]
%small
= link_to time_ago_in_words(comment.created_at), url_for(comment: comment.id, anchor: dom_id(comment))
%p [комментарий удален]
- else
- comments_user = comment.user
%strong.comments_user_line
= fa_icon "user-circle", class: "mr-1"
= link_to comments_user.nickname, user_path(comments_user), class: 'user_link'
%small
= link_to time_ago_in_words(comment.created_at), polymorphic_path(comment.commentable, comment: comment.id, anchor: dom_id(comment))
= simple_format comment.body
%div.comments_block
- if user_signed_in? && current_user.mute_to && current_user.mute_to > Time.current
- else
%small
- if user_signed_in?
%btn.reply Отвеить
- else
= link_to "Отвеить", new_user_session_path
= link_to "Удалить", [comment.commentable, comment], method: :delete, data: {confirm: "Вы уверены?"} if comment.user == current_user
= render partial: "comments/form", locals: { |
commentable: comment.commentable, |
parent_id: reply_to_comment_id(comment, nesting, max_nesting), |
class: "mt-4 d-none replies_form", |
target: "reply.form" |
} |
= tag.div id: "#{dom_id(comment)}_comments" do
- if continue_thread.present? && nesting >= continue_thread && comment.comments.any?
= link_to "Открыть ветку", url_for(comment: comment.id, anchor: dom_id(comment))
- else
= render comment.comments, continue_thread: continue_thread, nesting: nesting + 1, max_nesting: local_assigns[:max_nesting]
我的comments_controller.rb:
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
@comment = @commentable.comments.new(comment_params)
@comment.user = current_user
if @comment.save
respond_to do |format|
format.html { redirect_to @commentable }
format.js
end
else
redirect_to @commentable, alert: "Ошибка :("
end
end
def destroy
@comment = @commentable.comments.find(params[:id])
return false unless current_user.id == @comment.user_id
@comment.destroy
redirect_to @commentable
end
private
def comment_params
params.require(:comment).permit(:body, :parent_id)
end
end
如果我在 application.js 中取消注释 rails-ujs,如下所示:
require rails-ujs
然后当我发表评论时,它会说:
ActiveRecord::RecordNotFound in News::CommentsController#create
Couldn't find News with 'id'=artifact-v-spiske-liderov-prodazh-steam-po-itogam-2018-goda
Extracted source (around line #7):
6 def set_commentable
7 @commentable = News.find(params[:news_id])
8 end
9 end
我该如何解决?谢谢!
解决方案
第一个建议 -require rails-ujs
在您解决问题之前发表评论。调试起来会更容易。
然后,问题是params[:news_id]
您传递的是新闻 slug ( artifact-v-spiske-liderov-prodazh-steam-po-itogam-2018-goda
) 而不是新闻 id。一种解决方案是修改代码以将新闻 id 作为params[:news_id
] 传递。
另一个更丑陋的解决方案是将set_commentable
方法重构News::CommentsController
为如下所示:
def set_commentable
@commentable = News.find_by(slug: params[:news_id])
end
但是,它可能会破坏其他使用的地方set_commentable
。
推荐阅读
- asp.net-core-mvc - 如何在 asp.net-mvc-core 中的 asp-items 的 ViewBag 下拉列表中显示多个字段
- python - 如何创建 Matplotlib 小提琴图
- python - 动画轮廓和散点图
- sql-server - 每个 SQL select 语句都以文本格式生成
- datatables - 响应式数据表中的数据表父控件
- angular - AddThis 在 Angular 6/7 项目中不起作用
- php - 类 App\Http\Controllers\Community/CommunityContoller 不存在
- java - Java 8:DateTimeFormatter 不根据语言环境翻译时区
- mysql - 在 VB.NET 中,调用 conn.close 或 conn.dispose 甚至使用 USING 后 MySQL 连接不会关闭
- sql - 尝试使用多个值插入 SQL Server 时出现不正确的语法错误