【发布时间】: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