【发布时间】:2015-04-09 12:45:15
【问题描述】:
我正在阅读 Apress “Beginning Rails 4, 3rd edition”一书。本书通过逐步构建博客应用程序向您介绍 Rails。我已经完成了一半,收到以下错误消息:
ActiveModel::ForbiddenAttributesError in CommentsController#create
我已将此追溯到我的 cmets_controller.rb 文件,该文件如下所示:
class CommentsController < ApplicationController
before_filter :load_article
def create
@comment = @article.comments.new(params[:comment])
if @comment.save
redirect_to @article, :notice => 'Thanks for your comment'
else
redirect_to @article, :alert => 'Unable to add comment'
end
end
def destroy
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to @article, :notice => 'Comment deleted'
end
private
def load_article
@article = Article.find(params[:article_id])
end
end
具体来说,问题似乎是由第 5 行引起的:
@comment = @article.comments.new(params[:comment])
根据我收集到的信息,问题似乎在于我正在阅读的这本书是为早期版本的 Rails 编写的。我使用的是 Rails 4.2.0,看来我需要使用不同的语法。我需要进行哪些更改才能使我的代码正常工作?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-4.2