在我的评论应用程序中,当我刷新页面时,评论消失了

发布于 2024-12-09 07:53:28 字数 1227 浏览 2 评论 0原文

我的评论应用程序可以工作,但唯一的问题是每当我刷新页面时,评论就会消失。在日志中,它显示正文已插入评论表中(已保存)。我在这里做错了什么?任何帮助将不胜感激。先感谢您。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过潦 2024-12-16 07:53:28

我没有看到您的评论显示在您的节目模板中的什么位置。

像这样的事情怎么样?

   <div id="comments">
      <% @comments do |comment| %>
        <%= comment.body %>
      <% end %>
   </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 %>

注意,您需要在控制器中设置@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?

   <div id="comments">
      <% @comments do |comment| %>
        <%= comment.body %>
      <% end %>
   </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 %>

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| %>...

喜爱纠缠 2024-12-16 07:53:28

我猜想该评论与未分配的帖子有belongs_to关系。

在您的表单中,您应该添加

<%= f.hidden_field :post_id, @post.id %>

如果您想按书本操作,则 post_id 应该受到 attr_protected 并在评论控制器中手动分配

@comment = Comment.new(params[:comment])
@comment.post_id = params[:comment][:post_id]
@comment.save

I guess the comment has a belongs_to relationship with the post that is not assigned.

In your form you should add

<%= f.hidden_field :post_id, @post.id %>

If you want to play by the book, post_id should be attr_protected and instead assign it manually in the comments controller

@comment = Comment.new(params[:comment])
@comment.post_id = params[:comment][:post_id]
@comment.save
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文