0

我只是按照以下教程进行操作,效果很好。http://www.communityguides.eu/articles/6

然而,他对我来说很难的一件事就是编辑。

我打电话给我的link_to 编辑有跟随

<%= link_to 'Edit', edit_article_comment_path(@article, comment) %>

然后将我带到一个错误的页面,不知道为什么。

NoMethodError in Comments#edit

Showing /home/jean/rail/voyxe/app/views/comments/_form.html.erb where line #1 raised:

undefined method `comment_path' for #<#<Class:0xa8f2410>:0xb65924f8>
Extracted source (around line #1):

1: <%= form_for(@comment) do |f| %>
2:   <% if @comment.errors.any? %>
3:     <div id="error_explanation">
4:       <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

现在这里是评论中的表格编辑

<%= form_for(@comment) do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>
      <% @comment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这里是控制器文章控制器秀

 @article = Article.find(params[:id])
 @comments = @article.comments.find(:all, :order => 'created_at DESC')

评论控制器编辑

  def edit
    @comment = Comment.find(params[:id])
  end
4

1 回答 1

1

看起来它comment是一个嵌套资源,因此您需要指定包含article在其中的资源comment。例如:

<%= form_for [@article, @comment] do |f| %>

comment_path是一个未定义的方法,因为没有在顶层公开注释的路由。有时运行rake routes以查看可用的路线很有帮助。

更新:

您链接的文章仅提供评论createdelete操作。如果您需要支持编辑操作,那么您需要通过更改来修改路由:

resources :comments, :only => [:create, :destroy]  

至:

resources :comments, :only => [:create, :destroy, :edit, :update]  

您还需要实现编辑和更新操作 - 按照惯例,编辑将显示表单,更新将处理表单提交。您还需要确保它@article在您的编辑视图中可用。

于 2012-08-20T19:55:02.917 回答