2013-07-02 42 views
8

我在红宝石下面的代码,直接从Getting Started with 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我碰到下面的错误承担。

不能转换成符号串

+3

又有什么问题???? –

+3

什么是错误完全回溯?提供更多数据。 –

回答

31

好像你要使用强PARAMATERS。您收到此错误无法将符号转换为字符串,因为您尚未配置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 

documentation更多细节上的配置。

现在你的代码应该工作。

+5

这应该是足够的1.)将gem'strong_parameters'添加到您的gem文件并捆绑它。 – d1jhoni1b

+0

@ d1jhoni1b是正确的。我测试了它,只需重新启动服务器即可在捆绑后使用它。 –

0

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

+0

欢迎来到[so]。你能否扩展你的答案? –

1

人若使用Mongoid,您可以通过添加解决这个问题,下面以一个初始化:

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