在我的评论应用程序中,当我刷新页面时,评论消失了
我的评论应用程序可以工作,但唯一的问题是每当我刷新页面时,评论就会消失。在日志中,它显示正文已插入评论表中(已保存)。我在这里做错了什么?任何帮助将不胜感激。先感谢您。
View#show
<div id="comments"></div>
<%= form_for :comment,:remote => true,:url=> {:controller=>"comments",:action=>"create"},:html => { :id => 'new-comment'} do |f| %>
<%= f.text_area(:body) %>
<div class="errors"></div>
<%= f.submit "post" %>
<% end %>
Comment 控制器
class CommentsController < ApplicationController
respond_to :js
def create
@deal=Deal.find(1)
@comment [email protected](params[:comment])
@comment.save
respond_with( @comment, :layout => !request.xhr? )
end
def show
@comment=Comment.all
@deal=Deal.find(1)
@[email protected]
end
end
create.js.erb
$('#comments').append("escape_javascript(@comment.body)");
My comment app works ,but the only problem is whenever i refresh the page the comments disappear.In the log it shows the body is inserted in the comments table(it is saved).What am i doing wrong here?Any help will be appreciated.Thank you in advance.
View#show
<div id="comments"></div>
<%= form_for :comment,:remote => true,:url=> {:controller=>"comments",:action=>"create"},:html => { :id => 'new-comment'} do |f| %>
<%= f.text_area(:body) %>
<div class="errors"></div>
<%= f.submit "post" %>
<% end %>
Comment controller
class CommentsController < ApplicationController
respond_to :js
def create
@deal=Deal.find(1)
@comment [email protected](params[:comment])
@comment.save
respond_with( @comment, :layout => !request.xhr? )
end
def show
@comment=Comment.all
@deal=Deal.find(1)
@[email protected]
end
end
create.js.erb
$('#comments').append("escape_javascript(@comment.body)");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有看到您的评论显示在您的节目模板中的什么位置。
像这样的事情怎么样?
注意,您需要在控制器中设置
@comments
或使用其他获取注释的方法,例如@view = View.find(params[:id])
和<%= @view.comments.each |评论| %>...
I don't see where your comments are being display in your show template.
How about something like this?
Note, you need to set
@comments
in the controller or use another method of getting comments, e.g.@view = View.find(params[:id])
and<%= @view.comments.each do |comment| %>...
我猜想该评论与未分配的帖子有belongs_to关系。
在您的表单中,您应该添加
如果您想按书本操作,则 post_id 应该受到 attr_protected 并在评论控制器中手动分配
I guess the comment has a belongs_to relationship with the post that is not assigned.
In your form you should add
If you want to play by the book, post_id should be attr_protected and instead assign it manually in the comments controller