Ravi Prakash Singh
Ravi Prakash Singh

Reputation: 163

can't convert Symbol into String

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

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

can't convert Symbol into string

Upvotes: 9

Views: 8234

Answers (3)

Ben Yee
Ben Yee

Reputation: 1595

If anyone is using Mongoid, you can fix this issue by adding the following to an initializer:

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

Upvotes: 1

Sunny
Sunny

Reputation: 11

Add gem 'strong_parameters' to the gem file and run >bundle install in command prompt Refresh the browser.

Upvotes: 0

Amarnath Krishnan
Amarnath Krishnan

Reputation: 1263

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.

Configure 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 configuring.

Now your code should work.

Upvotes: 32

Related Questions