I was just trying out Ruby on Rails with the Getting Started with Rails tutorial. I followed all the steps but i keep getting the error TypeError in PostsController#create.
This happens when i'm at step 5.6 Saving data in the controller.
My PostsController.rb looks like this:
class PostsController < ApplicationController
def new
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
and i'm at localhost:3000/posts/new. I'm requesting a POST and it fails with the following:
can't convert Symbol into String
app/controllers/posts_controller.rb:15:in `post_params'
app/controllers/posts_controller.rb:7:in `create'
This error occurred while loading the following files: post
You can find all my code on my GitHub repo.
Please help :(
You are using rails version 3.2.xx, on rails version 3.2.xx not include strong_parameters gem
Note that def post_params is private. This new approach prevents an attacker from setting the model's attributes by manipulating the hash passed to the model. For more information, refer to this blog post about Strong Parameters.
Add gem "strong_parameters"
to your Gemfile, then run bundle install
include ActiveModel::ForbiddenAttributesProtection
on your model or create config/initializers/strong_parameters.rb
and put this :
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection))
config.active_record.whitelist_attributes = false
in config/application.rb
https://github.com/rails/strong_parameters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With