Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError in Getting Started with Rails tutorial

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 :(

like image 729
Hunor Kovács Avatar asked Jul 01 '13 11:07

Hunor Kovács


1 Answers

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.


  1. Add gem "strong_parameters" to your Gemfile, then run bundle install

  2. include ActiveModel::ForbiddenAttributesProtection on your model or create config/initializers/strong_parameters.rb and put this :

    ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection))

  3. config.active_record.whitelist_attributes = false in config/application.rb

https://github.com/rails/strong_parameters

like image 90
rails_id Avatar answered Sep 26 '22 13:09

rails_id