Cannot convert character to string

I have the following code in Ruby, right from the Rails Getting Started 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 

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

cannot convert character to string

+8
ruby ruby-on-rails-3
source share
3 answers

It looks like you are trying to use strong options . You get this error; you cannot convert a character to a string because you did not configure strong_parameters. Therefore, by default, you cannot use params parameters with characters.

Set strong parameters as follows:

 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 customization.

Your code should now work.

+31
source share

If someone uses Mongoid, you can fix this problem by adding the following to the initializer:

 Mongoid::Document.send(:include, ActiveModel::ForbiddenAttributesProtection) 
+1
source share

Add gem 'strong_parameters' to the gem file and run> bundle install at the command line Update your browser.

0
source share

All Articles