无法将符号转换为字符串

Rav*_*ngh 9 ruby ruby-on-rails-3

我在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
Run Code Online (Sandbox Code Playgroud)

当我运行上面Create的内容时,我收到以下错误.

无法将符号转换为字符串

Ama*_*nan 32

看起来你正试图使用强大的参数.您得到此错误无法将符号转换为字符串,因为您尚未配置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
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这应该足够了.)将gem"strong_parameters"添加到你的gemfile并捆绑它. (5认同)