Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道4。使用嵌套资源设置注释编辑功能_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 轨道4。使用嵌套资源设置注释编辑功能

Ruby on rails 轨道4。使用嵌套资源设置注释编辑功能,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有以下型号:用户、产品和评论。用户可以添加、编辑和删除对产品的评论。我已经成功地设置了添加和删除功能,现在我正在努力编辑,由于某些原因,这给我带来了一些困难 当我单击编辑注释链接时,我的当前代码返回此错误: 命名错误位于/products/800/comments/8/edit nil:NilClass的未定义方法“comments” 下面是我的评论模型的样子: # id :integer not null, primary key # body

我有以下型号:
用户
产品
评论
。用户可以添加、编辑和删除对产品的评论。我已经成功地设置了添加和删除功能,现在我正在努力编辑,由于某些原因,这给我带来了一些困难

当我单击
编辑注释
链接时,我的当前代码返回此错误:

命名错误位于/products/800/comments/8/edit

nil:NilClass的未定义方法“comments”


下面是我的评论模型的样子:

#  id         :integer          not null, primary key
#  body       :text
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  user_id    :integer
#  product_id :integer

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :product
  # validations ...
end
  def show
    product = Product.find(params[:id])
    photos = ProductsPhoto.where(product: product)
    case product.products_category.name
    when 'Violin'
      @product = [product, Violin.where(product: product).first, photos]
    when 'Guitar'
      @product = [product, Guitar.where(product: product).first, photos]
    when 'Saxophone'
      @product = [product, Saxophone.where(product: product).first, photos]
    when 'Piano'
      @product = [product, Piano.where(product: product).first, photos]
    end
    @comment = Comment.new
    @comments = Comment.where(product_id: product.id).order('created_at DESC')
  end
My
ProductsController
只有一个方法
show
,没有其他方法,下面是它的外观:

#  id         :integer          not null, primary key
#  body       :text
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  user_id    :integer
#  product_id :integer

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :product
  # validations ...
end
  def show
    product = Product.find(params[:id])
    photos = ProductsPhoto.where(product: product)
    case product.products_category.name
    when 'Violin'
      @product = [product, Violin.where(product: product).first, photos]
    when 'Guitar'
      @product = [product, Guitar.where(product: product).first, photos]
    when 'Saxophone'
      @product = [product, Saxophone.where(product: product).first, photos]
    when 'Piano'
      @product = [product, Piano.where(product: product).first, photos]
    end
    @comment = Comment.new
    @comments = Comment.where(product_id: product.id).order('created_at DESC')
  end
现在是我的
评论控制器
,它有
创建
编辑
更新
销毁

class CommentsController < ApplicationController
  def create
    @product = Product.find(params[:product_id])
    @comment = @product.comments.create(comment_params)
    @comment.user_id = current_user.id
    if @comment.save
      redirect_to @product, notice: 'Comment Created!'
    else
      redirect_to @product, notice: 'Something went wrong...'
    end
  end

  def show
  end

  def edit
    @product = Product.find(params[:product_id])
    @comment = @product.comments.find(params[:id])
  end

  def update
    @product = Product.find(params[:product_id])
    @comment = @product.comments.find(params[:id])
    respond_to do |format|
      if @comment.update_attributes(comment_params)
        format.html do
          redirect_to [@comment.product, @comment], notice: 'Comment Updated!'
        end
      else
        format.html { render action: 'edit', notice: 'Something went wrong...' }
      end
    end
  end

  def destroy
    @product = Product.find(params[:product_id])
    @comment = @product.comments.find(params[:id])
    @comment.destroy!
    redirect_to @product, notice: 'Comment Deleted!'
  end

  private

  def comment_params
    params.require(:comment).permit(:body)
  end
end
视图/products/show.html.erb
中,我呈现了部分注释,其中有用于销毁和编辑注释的链接,它们如下所示:

<%= link_to edit_product_comment_path(comment.product, comment) do %>
  <span class="glyphicon glyphicon-pencil"></span>
<% end %>
<%= link_to [comment.product, comment], method: :delete do %>
  <span class="glyphicon glyphicon-remove"></span>
<% end %>
在我的
views/comments/edit.html.erb
中,我呈现了相同的表单:

  <%= render 'products/form' %>

但是,当我单击“编辑”链接时,出现以下错误:

命名错误位于/products/800/comments/8/edit

nil:NilClass的未定义方法“comments”

\u form.html.erb
的第一行


我希望我已经提供了足够的信息来描述这个问题


那么,你能帮我解决评论编辑的问题吗?

今天早上太早了

这是不正确的:

  <%= simple_form_for([@product, @product.comments.build]) do |f| %>

尝试:


添加了部分

(所有这些都假设我们使用的是单个@comment,而不是@comments集合。基于您对删除工作的评论。)

要回复您的评论,是的,您绝对需要为您的子表“新建”和“编辑”操作使用单独的表单

表单“新”命名约定为comments/_Form.html.erb,“编辑”表单为comments/_Form_edit.html.erb

我会打开“新”的行动表格征求意见

<%= simple_form_for([:product, @comment]) do |f| %>

还有“编辑”动作

<%= simple_form_for [@comment.product_id, @comment],  url: product_comment_path(@comment.product_id, @comment)  do |f| %>

很抱歉,我忘记了上一版本中的URL,我已经更新了上面的代码片段以反映此更改。警告:可能还有其他方法来构建此链接,但这就是我如何实现一个产品(父产品)和评论(许多子产品)的案例

嵌套属性


在进一步回应您的评论时,我相信您正在寻找的一些内容可以通过在表单中使用嵌套属性来实现。这是另一个话题。我会让新的/编辑表单简单案例工作,然后增加复杂性

@Pavan这让我在同一个地方犯了同样的错误…:(你试过在Comments controller中调试你的
edit
操作以查看是否找到了该产品吗?@Nathan我不确定该如何调试该操作。我无法访问
update
方法,但我可以调试
edit
,并且可以正确找到
@product
@comment
变量。呵呵您是否调试了编辑操作以了解@product正在设置?正在设置
@product
@comment
。我在
编辑
操作结束时使用了
byebug
。如果您尝试一下,请让我知道它是如何进行的。这给了我
未定义的方法注释。\u路径
。请查看更新的代码在我的
\u form.html.erb
中,
ProductsController
中的
@product
变量是数组,其中第一个元素是
product
对象。我应该如何更改当前表单以使
编辑
操作工作?也许我应该使用不同的表单来创建注释(如在
views/products/_-form.html.erb
)和编辑(如在
views/comments/_-form.html.erb
)?因为
@product
中的
CommentsController
是一个
product
对象,而
ProductsController
中的
@product
是一个数组。我遗漏了一些内容。很抱歉。添加了部分来回答。我按照您说的做了,编辑表单实际工作了!但是现在,当我在编辑评论时单击submit按钮时,我得到了
products/:id/comments/:id的路由错误
现在我已经在
comments\u controller
中更改了重定向,一切都很好。感谢您的详细解释和正确答案!
<%= simple_form_for([:product, @comment]) do |f| %>
<%= simple_form_for [@comment.product_id, @comment],  url: product_comment_path(@comment.product_id, @comment)  do |f| %>