加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Rails上创建一个博客ruby – 问题删除注释

发布时间:2020-12-16 22:19:28 所属栏目:百科 来源:网络整理
导读:像我一直输入的,我一直很喜欢rails和编程,所以很容易.提前致谢. 我已经成功地遵循了Ryan Bates在how to build a weblog in 15 minutes的初步教程.如果你不知道这个教程让你通过创建帖子并允许对这些帖子发表评论.它甚至通过在post.html.erb页面上创建和显示
像我一直输入的,我一直很喜欢rails和编程,所以很容易.提前致谢.

我已经成功地遵循了Ryan Bates在how to build a weblog in 15 minutes的初步教程.如果你不知道这个教程让你通过创建帖子并允许对这些帖子发表评论.它甚至通过在post.html.erb页面上创建和显示注释来引入AJAX.一切都很好.

这是打嗝,当Ryan带你通过本教程他清除comments_controller并且仅显示用于创建评论的代码.我试图添加编辑和销毁评论的能力.似乎不能让它工作,不断删除实际的帖子不是评论(日志显示我不断发送DELETE请求到PostsController).这是我的代码:

class CommentsController < ApplicationController
 def create
   @post = Post.find(params[:post_id])
   @comment = @post.comments.create!(params[:comment])
   respond_to do |format|
     format.html { redirect_to @post }
     format.js
   end
 end

 def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml  { head :ok }
    end
  end
end

/views/posts/show.html.erb

<%= render :partial => @post %>

    <p>
        <%= link_to 'Edit',edit_post_path (@post) %> |
        <%= link_to 'Destroy',@post,:method => :delete,:confirm => "Are you sure?" %> |
        <%= link_to 'See All Posts',posts_path %>
    </p> 

    <h2>Comments</h2>
    <div id="comments">
        <%= render :partial => @post.comments %>
    </div>

    <% remote_form_for [@post,Comment.new] do |f| %>
        <p>
            <%= f.label :body,"New Comment" %><br/>
            <%= f.text_area :body %>
        </p>
        <p>

<%= f.submit "Add Comment" %></p>
<% end %>

/views/comments/_comment.html.erb

<% div_for comment do %>
    <p>
        <strong>Posted <%= time_ago_in_words(comment.created_at) %> ago
        </strong><br/>
        <%= h(comment.body) %><br/>
        <%= link_to 'Destroy',@comments,:confirm => "Are you sure?" %>
    </p>
<% end %>

的routes.rb

ActionController::Routing::Routes.draw do |map|
  map.resources :posts,:has_many => :comments
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

解决方法

meagar是正确的路径,但由于这是一个嵌套的路线你必须做的:

<%= link_to'Destroy',[@post,comment],...%>

所以,你正在传递评论和帖子,让栏杆根据你的定义找出路线.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读