Link_to轨道嵌套表单编辑



我刚刚学习了下面的教程,效果很好。http://www.communityguides.eu/articles/6

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

我打电话给我的链接编辑已经关注

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

看起来comment是一个嵌套资源,因此需要指定包含commentarticle。例如:

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

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

更新:

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

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

至:

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

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

最新更新