无法将符号转换为字符串 [英] can't convert Symbol into String

查看:37
本文介绍了无法将符号转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code in Ruby, take directly from the Getting Started with Rails guide

 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 时,出现以下错误.

When I run the above Create I get the following error.

无法将符号转换为字符串

can't convert Symbol into string

推荐答案

您似乎正在尝试使用强参数.您收到此错误无法将符号转换为字符串,因为您尚未配置 strong_parameters.因此,默认情况下,您不能在带有符号的参数上使用 require.

It seems like you are trying to use strong paramaters. You get this error cannot convert symbol into string because you have not configured the strong_parameters. So by default you cant use require on params with symbols.

配置强参数如下:

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

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

See the documentation for more details on configuring.

现在您的代码应该可以工作了.

Now your code should work.

这篇关于无法将符号转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆