无法将符号转换为字符串

我在Ruby中有以下代码,可直接从Rails入门指南中获取

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

当我运行上面的Create出现以下错误。

无法将符号转换为字符串

看起来你正试图使用强大的参数 。 您得到此错误无法将符号转换为字符串,因为您尚未配置strong_parameters。 因此,默认情况下,您无法使用带符号的params。

配置强参数如下:

 1.) Add gem 'strong_parameters' to your gemfile and bundle it. 2.) Include Restrictions to you model as follows. include ActiveModel::ForbiddenAttributesProtection to your model. 3.) Disable white listing in application confiuration(config/application.rb) config.active_record.whitelist_attributes = false 

有关配置的更多详细信息,请参阅文档 。

现在你的代码应该工作了。

如果有人使用Mongoid,您可以通过将以下内容添加到初始化程序来解决此问题:

 Mongoid::Document.send(:include, ActiveModel::ForbiddenAttributesProtection) 

将gem“strong_parameters”添加到gem文件并在命令提示符下运行> bundle install刷新浏览器。