【发布时间】:2015-08-30 12:48:55
【问题描述】:
我有以下型号:User、Product 和 Comment。用户可以对产品添加、编辑和删除 cmets。我已经成功设置了添加和删除功能,现在我正在为编辑而苦苦挣扎,由于某种原因,这给我带来了很多困难。
当我点击edit comment 链接时,我当前的代码返回此错误:
/products/800/cmets/8/edit 处的 NoMethodError
nil:NilClass 的未定义方法 `cmets'
这是我的评论模型的样子:
# 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
在User 模型中我有has_many :comments 和Product - has_many :comments, dependent: :destroy。
在我的routes.rb 文件中,我有以下嵌套资源:
resources :products, only: [:show] do
resources :comments, only: [:create, :edit, :update, :destroy]
end
我的 ProductsController 有唯一的方法 show 没有别的,它的样子如下:
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
现在这是我的CommentsController,其中有create、edit、update 和destroy:
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
我的_form 视图位于views/products/_form.html.erb,如下所示:
<%= simple_form_for([@product[0], @product[0].comments.build]) do |f| %>
<%= f.error_notification %>
<%= f.input :body, required: true, placeholder: 'Type in your comment...', input_html: { class: 'form-control'}, label: false %>
<%= f.button :submit, class: 'btn btn-primary btn-block' %>
<% end %>
而在views/products/show.html.erb我渲染了cmets的部分,并且有销毁和编辑评论的链接,它们看起来像这样:
<%= 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 %>
删除链接可以正常工作,而编辑链接不行。
可能我弄错了路线,这里是cmets的路线:
product_comments POST /products/:product_id/comments(.:format) comments#create
edit_product_comment GET /products/:product_id/comments/:id/edit(.:format) comments#edit
product_comment PATCH /products/:product_id/comments/:id(.:format) comments#update
PUT /products/:product_id/comments/:id(.:format) comments#update
DELETE /products/:product_id/comments/:id(.:format) comments#destroy
在我的views/comments/edit.html.erb 中,我呈现相同的表单:
<%= render 'products/form' %>
但是,当我单击编辑链接时,我收到以下错误:
/products/800/cmets/8/edit 处的 NoMethodError
nil:NilClass 的未定义方法 `cmets'
_form.html.erb 的第一行。
我希望我已经提供了足够的信息来描述问题。
那么,您能帮我解决 cmets 编辑的问题吗?
【问题讨论】:
-
@Pavan 这在同一个地方给了我完全相同的错误...... :(
-
您是否尝试在 Comments 控制器中调试您的
edit操作以查看是否找到了产品? -
@Nathan 我不确定我应该如何调试它。我无法访问
update方法,但我可以调试edit,并且可以正确找到@product和@comment变量。 -
您如何调试编辑操作以了解正在设置@product?
-
@product和@comment正在设置中。我在edit操作的末尾使用了byebug
标签: ruby-on-rails ruby-on-rails-4