我有2种不同的帖子类型(新闻和文章)。当我浏览新闻并尝试评论任何新闻时,什么也没有发生。在Google Chrome控制台中,它说:
Arrays.stream(Moon.values())
.filter(m -> m.getPlanet().equals(this))
...
但是!当我尝试在文章类型中发表评论时,所有评论都很好。有什么问题吗?
我的_form.html.haml:
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
我的_comment.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"
我的comments_controller.rb:
- 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]
如果我像这样在application.js中取消注释rails-ujs:
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
然后当我发表评论时会说:
require rails-ujs
我该如何解决?谢谢!
答案 0 :(得分:0)
第一个建议-评论require rails-ujs
,直到解决问题为止。调试起来会更容易。
然后,问题是,作为params[:news_id]
的您传递的是新闻(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
的地方。