入门Rails教程中的TypeError



我刚刚在Rails入门教程中试用RubyonRails。我遵循了所有步骤,但在PostsController#create中不断出现错误TypeError。

当我在步骤5.6将数据保存在控制器中时,就会发生这种情况。

我的PostsController.rb看起来像这样:

class PostsController < ApplicationController
  def new
  end
  def create
    @post = Post.new(post_params)
    @post.save
    redirect_to @post
  end
  private
  def post_params
    params.require(:post).permit(:title, :text)
  end
end

我在localhost:3000/posts/new。我正在请求POST,但由于以下原因而失败:

无法将Symbol转换为字符串

app/controllers/posts_controller.rb:15:in `post_params'
app/controllers/posts_controller.rb:7:in `create'

加载以下文件时发生此错误:post

你可以在我的GitHub repo上找到我所有的代码。

请帮助:(

您使用的是rails 3.2.xx版本,在rails 3.2.xx版本上不包括strong_parameters gem

请注意,def-post_params是私有的。这种新方法防止了攻击者通过操纵哈希设置模型的属性传递给模型。有关更多信息,请参阅此博客文章关于强参数。


  1. gem "strong_parameters"添加到Gemfile,然后运行bundle install

  2. 在你的模型上包括ActiveModel::ForbiddenAttributesProtection或创建config/initializers/strong_parameters.rb,并放上这个:

    ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection))

  3. config/application.rb 中的config.active_record.whitelist_attributes = false

https://github.com/rails/strong_parameters

相关内容

最新更新