使用 UI 删除我的博客文章时遇到问题

发布于 2024-12-20 03:45:45 字数 1869 浏览 2 评论 0原文

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

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

我一直在努力使其顺利运行,但最近意识到“索引”页面上的删除按钮不起作用。我没有提示确认删除,而是直接进入相关帖子的“显示”。

这是索引“index”页面的片段:

<% @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 中与删除相关的代码片段:

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 的“show”操作

作为参考,“删除评论”按钮的代码是:

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

comments_controller 中的代码片段是:

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

我缺乏对 RoR 如何工作以及这些文件如何工作的完整了解相互充分互动以解决问题,因此我们将非常感谢任何帮助。

I am very new to Ruby on Rails so apologies, as this may be a silly question to post here.

I have made a blog (using generate scaffold). I have a few pages to interact and edit blog posts, starting with a main page which displays all blog posts ("index"), an area to view a specific blog post ("show"), and area to edit a blog post ("edit"), and an area to create a new blog post ("new").
I've also created comments (again using generate scaffold) to be applied to relevant blog posts. Comments, and a partial form for comments appears on the "show" page.

I have been working away on the whole thing to get it working nicely, but have recently realised that delete buttons that I had on the "index" page aren't working. Instead of prompting confirmation for the delete, I'm simply taken to the "show" of the relevant post.

Here is a snippet of the index "index" page:

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

And here is the snippet of code from the posts_controller relevant to the delete:

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

I have also found that the remove comment buttons (on the "show" page alongside each comment) have stopped working with an error message:

Unknown action

The action 'show' could not be found for CommentsController

For reference the code for the "remove comment" button is:

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

And the snippet of code in the comments_controller is:

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

I lack the full knowledge of how RoR works, and how these files interact with each other fully to troubleshoot the problem, so any help would be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

始终不够爱げ你 2024-12-27 03:45:45

首先,您错过了销毁链接方法调用中的箭头
更正于此。

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

First you miss the arrow in the destroy link method call
Correct it to this.

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

Second you don't have to find the post of a comment to delete it simply do this.

If you want to be redirected back to the post, you do it in the controller

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

in the 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
泅人 2024-12-27 03:45:45

我发现了问题 - 我自定义了样式表、javascript 文件等的链接,在执行此操作时,我遗漏了脚手架创建的帖子和评论的 javascript 文件的链接。

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

I've found the problem - I had customised the links to stylesheets, javascript files etc and in doing this I had left out links to the javascript files for posts and comments created by the scaffold.

After including links to these files everything worked again.

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