【发布时间】:2018-12-24 22:21:21
【问题描述】:
评论控制器
class CommentsController < ApplicationController
before_action :load_commentable
before_action :checked_logged_in, only: [ :create]
def new
@comment = @commentabl.comments.new
end
def create
@comment = @commentable.comments.new(comment_params)
@comment.user_id = current_user.id
@comment.commenter = current_user.username
if @comment.blank? || @comment.save
flash[:success] = "Commented was created"
ActionCable.server.broadcast 'comment_channel',
commenter: current_user.username,
comment: @comment.content
redirect_to @commentable
else
flash[:danger] = render_to_string(:partial => 'shared/error_form_messaging',
:locals => {obj: @comment},
format: :html)
redirect_to @commentable
end
end
private
def comment_params
params.require(:comment).permit(:content, :commenter, :user_id)
end
def load_commentable
resource, id = request.path.split('/')[1,2]
@commentable = resource.singularize.classify.constantize.find(id)
end
def checked_logged_in
unless logged_in?
flash[:danger] = 'please log in to be able to comment'
redirect_to login_path
end
end
end
我的评论表单:
<%= form_with model:[commentable, commentable.comments.new], :html => {class: "form-horizontal", role:"form"} , local: true do |form| %>
<div class="form-group">
<div class="control-label col-sm-2">
<%= form.label :content, 'Comment' %>
</div>
<div class="col-sm-8">
<%= form.text_field :content , class: 'form-control', placeholder: "enter your comment here", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%= form.submit 'Comment' , class: ' btn btn-primary' %>
</div>
</div>
<% end %>
表单在show.html.erb中调用
<h2 class="text-center">
Title: <%= @article.title %>
</h2>
<div class="well col-xs-8 col-xs-offset-2">
<div id="user-info-showpage" align="center">
Created by: <%= render 'shared/user-info', obj: @article.user %>
</div>
<h4 class="text-center">
<strong>Description:</strong>
</h4>
<hr />
<%= simple_format(@article.description) %>
<% if @article.categories.any? %>
<p>Categories: <%= render @article.categories %></p>
<% end %>
<div class="article-actions">
<% if logged_in? && (current_user == @article.user || current_user.admin?) %>
<%= link_to "Delete", article_path(@article), method: :delete,
data: {confirm: "Are you sure you want to delete the article?"},
class: 'btn btn-xs btn-danger' %>
<%= link_to "Edit", edit_article_path(@article), class: 'btn btn-xs btn-success'%>
<%end%>
<%= link_to "View All Articles", articles_path , class: 'btn btn-xs btn-primary'%>
</div>
</div>
<% if logged_in? %>
<div class="col-xs-8 col-xs-offset-2">
<%#= render partial: 'comments/form', :locals => {commentable: @article} %>
</div>
<%end%>
<div class="col-xs-8 col-xs-offset-2">
<div id="comments"></div>
<%= @article.comments.inspect %>
<% @article.comments.each do |c| %>
<div class="well">
<%= c.content %> by
<%= c.commenter %>
</div>
<%end%>
<div id="comments"></div>
</div>
如果需要更多信息,请询问我,以便我提供
注意:我不确定这个空记录是由于 commentable.cmets 为 nil 还是我错过了什么
我在show page评论了render form,现在空记录不见了,所以我的问题一定与form_with有关
【问题讨论】:
-
您能出示您的
show.html.erb代码吗?顺便说一句,commentable.comments.new应该是commentable.comments.build -
感谢您的回复。我用构建测试并得到相同的结果。我发布了显示页面的整个代码。 是新的构建别名吗?所以它们应该是相同的,不是吗?
-
为什么需要在表单中输入
<%= @article.comments.inspect %>?不想看到的可以删除#<ActiveRecord... -
用于调试目的。当我解决问题时,我会删除它。
-
@KickButtowski 你能分享控制器的代码吗
标签: ruby-on-rails ruby ruby-on-rails-5