2013-07-02 41 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)