简体   繁体   English

Link_to Rails嵌套表单编辑

[英]Link_to rails nested form edit

I just followed the following tutorial and works great. 我只是按照以下教程操作,效果很好。 http://www.communityguides.eu/articles/6 http://www.communityguides.eu/articles/6

However one thing his hard for me and that is an edit. 然而,对我而言,他很难做的一件事就是编辑。

I called my link_to edit has follow 我称我的link_to编辑如下

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

Which then bring me to a page with error and not sure why. 然后将我带到错误页面,不确定原因。

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>

Now here the form in comments edit 现在在这里评论中的表格

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

Here are the controller Article controller show 这是控制器文章控制器显示

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

Comment controller edit 评论控制器

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

Looks like it comment is a nested resource so you need to specify the article in which the comment is contained. 看起来comment是一个嵌套资源,因此您需要指定其中包含该commentarticle For example: 例如:

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

comment_path is an undefined method because there is no route that exposes comments at the top-level. comment_path是未定义的方法,因为没有路由可以在顶级公开注释。 Its sometimes helpful to run rake routes to see what routes are available. 有时可能需要运行rake routes以查看可用的路线。

UPDATE: 更新:

The article you linked only provides create and delete actions for comments. 您链接的文章仅提供createdelete评论操作。 If you need to support edit operations then you would need to modify the routes by changing: 如果需要支持编辑操作,则需要通过更改以下内容来修改路线:

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

to: 至:

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

You would also need to implement the edit and update actions - by convention edit will display the form and update will process the form submission. 您还需要实现编辑和更新操作-按照惯例,edit将显示表单,而update将处理表单提交。 You will also need to make sure that @article is available in your edit view. 您还需要确保@article在您的编辑视图中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM