Link_to rails 嵌套表单编辑 [英] Link_to rails nested form edit

查看:74
本文介绍了Link_to rails 嵌套表单编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但是他对我来说很难的一件事就是编辑.

我打电话给我的link_to编辑已关注

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

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

NoMethodError in Comments#edit显示/home/jean/rail/voyxe/app/views/comments/_form.html.erb 其中第 1 行提出:#<#<Class:0xa8f2410>:0xb65924f8>的未定义方法`comment_path'提取的源代码(围绕第 1 行):1: <%= form_for(@comment) 做 |f|%>2: <% if @comment.errors.any?%>3:<div id="error_explanation">4:<h2><%=pluralize(@comment.errors.count, "error") %>禁止保存此评论:</h2>

现在在这里编辑评论中的表单

<%= form_for(@comment) do |f|%><% if @comment.errors.any?%><div id="error_explanation"><h2><%=pluralize(@comment.errors.count, "error") %>禁止保存此评论:</h2><ul><% @comment.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%><div class="field"><%= f.label :title%><br/><%= f.text_field :title %>

<div class="actions"><%= f.submit %>

<%结束%>

这是控制器文章控制器显示

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

评论控制器编辑

 定义编辑@comment = Comment.find(params[:id])结尾

解决方案

看起来 comment 是一个嵌套资源,所以你需要指定 article包含代码>注释.例如:

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

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

更新:

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

resources :comments, :only =>[:创建,:销毁]

到:

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

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

I just followed the following tutorial and works great. 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 '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. 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. Its sometimes helpful to run rake routes to see what routes are available.

UPDATE:

The article you linked only provides create and delete actions for comments. 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. You will also need to make sure that @article is available in your edit view.

这篇关于Link_to rails 嵌套表单编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆