【问题标题】:Trouble with deleting my blog posts using the UI使用 UI 删除我的博客文章时遇到问题
【发布时间】:2011-12-06 11:15:50
【问题描述】:

我对 Ruby on Rails 很陌生,因此很抱歉,因为在此处发布这可能是一个愚蠢的问题。

我做了一个博客(使用生成脚手架)。我有几个页面可以交互和编辑博客文章,从显示所有博客文章的主页(“索引”)、查看特定博客文章的区域(“显示”)和编辑博客文章的区域开始( “编辑”)和一个创建新博客文章的区域(“新”)。 我还创建了用于相关博客文章的 cmets(再次使用生成脚手架)。评论,cmets的部分表格出现在“show”页面。

我一直在努力让它正常工作,但最近意识到我在“索引”页面上的删除按钮不起作用。而不是提示确认删除,我只是被带到相关帖子的“显示”。

这里是索引“index”页面的sn-p:

<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= image_tag "work/thumbnails/#{post.image}", :alt => '' %></td>
<td><%= time_ago_in_words(post.created_at) %> ago (<%= post.created_at %>)</td>
<td class="zero-out"><%= link_to 'Show', post, :class => "submit-button" %></td>
<td class="zero-out"><%= link_to 'Edit', edit_post_path(post), :class => "submit-button" %></td>
<td class="zero-out"><%= link_to 'Destroy', post, confirm: 'Are you sure?', :method => :delete, :class => "submit-button" %></td>
</tr>
<% end %>

这里是posts_controller中与删除相关的sn-p代码:

def destroy
@post = Post.find(params[:id])
@post.destroy

respond_to do |format|
  format.html { redirect_to posts_url }
  format.json { head :ok }
end
end

我还发现删除评论按钮(在每条评论旁边的“显示”页面上)已停止工作并出现错误消息:

未知操作

无法为 CommentsController 找到“显示”操作

“删除评论”按钮的代码供参考:

<%= link_to 'Remove Comment', [comment.post, comment],
    :confirm => 'Are you sure?',
    :method => :delete %>

而cmets_controller中代码的sn-p为:

def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end

我不完全了解 RoR 的工作原理,以及这些文件如何完全相互交互以解决问题,因此非常感谢任何帮助。

【问题讨论】:

  • 你的 config/routes.rb 是什么样的?
  • 我假设您已遵循本教程 guides.rubyonrails.org/getting_started.html 。正确执行所有步骤或在此处发布代码,以便我们获得更好的图片
  • 在我的 routes.rb 中没有提到与博客有关的任何内容。这是为了让公众查看我的网站。是的,我部分地遵循了本教程并正确地遵循了它......自从我停止使用它以来,显然发生了一些变化,但我最好知道为什么会出现这个错误,以便我可以成功继续。

标签: ruby-on-rails ruby database blogs


【解决方案1】:

首先你错过了销毁链接方法调用中的箭头 更正一下。

<%= link_to 'Destroy', post, confirm: 'Are you sure?', :method => :delete, :class => "submit-button" %>

其次,您不必找到评论的帖子即可将其删除,只需执行此操作即可。

如果你想被重定向回帖子,你可以在控制器中完成

<%= link_to 'Remove comment', comment, confirm: 'Are you sure?', :method => :delete %>

在comment_controller中

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

    respond_to do |format|
      format.html { redirect_to @post }
      format.json { head :ok }
    end
  end

【讨论】:

  • 谢谢,但恐怕这些都不起作用。提到 cmets 与帖子相关可能会很有用,所以我猜这就是为什么有 post.comment 而不仅仅是评论......
  • 那太糟糕了!我必须看到更多你的代码。放入完整的帖子索引并显示视图
  • 我已经编辑了我上面的消息以包含包含删除按钮的“索引”页面中的代码。你还需要什么?我必须问发布“显示”页面代码的重要性是什么?...理想情况下,这个过程甚至不应该涉及“显示”页面。
【解决方案2】:

我发现了问题 - 我已经自定义了指向样式表、javascript 文件等的链接,在此过程中,我遗漏了指向脚手架创建的帖子和 cmets 的 javascript 文件的链接。

在包含这些文件的链接后,一切都恢复正常了。

【讨论】:

    猜你喜欢
    • 2021-04-22
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多