ruby-on-rails - ActiveModel::ForbiddenAttributesError

标签 ruby-on-rails rubygems

我一直在遵循创建和安装引擎的 Rails 指南 here创建博客文章,当我尝试发表评论时,它返回“ActiveModel::ForbiddenAttributesError in Blorgh::CommentsController#create”错误。 评论 Controller

    require_dependency "blorgh/application_controller"

module Blorgh
  class CommentsController < ApplicationController
    def create
      @post = Post.find(params[:post_id])
      @comment = @post.comments.create(params[:comment])
      flash[:notice] = "Comment has been created!"
      redirect_to posts_path
    end
  end
end

这是评论模型

 module Blorgh
  class Comment < ActiveRecord::Base

  end
end

如何解决这个问题?

最佳答案

我猜你使用的是rails 4。你需要标记所有必需的参数 就这样:

   def create
      @post = Post.find(params[:post_id])
      @comment = @post.comments.create(post_params)
      flash[:notice] = "Comment has been created!"
      redirect_to posts_path
    end

    def post_params
      params.require(:blorgh).permit(:comment)
    end

希望 this link 帮助...

关于ruby-on-rails - ActiveModel::ForbiddenAttributesError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19509573/

相关文章:

ruby - Net :Http and third-party library for making API calls in Rails/Ruby?有什么区别

ruby-on-rails - 电影院预订申请

html - Bootstrap 是否覆盖了 Ruby on Rails 4 网站中的自定义 CSS?

ruby-on-rails - 加载错误 : cannot load such file -- active_support

ruby-on-rails - 为什么 ActiveRecord/PostgreSQL 在数据库中找不到我的数据?

Ruby:有关于线程池的 gem 吗?

ruby - Unicorn/KGIO 无法在带 ruby​​ 1.9.3 的 Mountain Lion 上安装

ruby-on-rails - 切换到 bundler 后如何消除 gem 错误

ruby-on-rails - API/JSON : Can't verify CSRF token authenticity

ruby-on-rails - 开发环境下使用Rails 3发送邮件