Not been able to insert data onto DB

Hi all,
I am not been able to insert row into DB from the rails console.

Command used:
Post.create(:title =>"t1",:content =>"c1")

Output :
(0.2ms) begin transaction
  SQL (0.4ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES
(?, ?) [["created_at", "2015-04-06 19:21:40.219007"], ["updated_at",
"2015-04-06 19:21:40.219007"]]
   (204.6ms) commit transaction
=> #<Post id: 13, title: nil, content: nil, created_at: "2015-04-06
19:21:40", updated_at: "2015-04-06 19:21:40">

title and content column are not populates.

create_posts.rb file :

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.text :content

      t.timestamps null: false

Did u add these attributes in post.rb as attr_accesble

amruby wrote in post #1171533:

Did u add these attributes in post.rb as attr_accesble

Yeah I added these attribute in post.rb file.

post.rb file:

class Post < ActiveRecord::Base
  attr_accessor :title, :content

attr_accessible is not the same as attr_accessor


attr_accessible, not attr_accessor, that @colin Law mentioned.

I tried attr_accessible, but it seems this one is also not working.

When I am adding the data onto Db through the console then it is giving
error, NoMethodError: undefined method `attr_accessible' for Post (call
'Post.connection' to establish a connection):Class

Found a solution for this one.

I just removed the "attr_accessible :title, :content" line from post.rb
and now I am able to insetr data onto DB through console.

post.rb file:

class Post < ActiveRecord::Base

However, I made a sample blogging application in my system. I am able to
insert a record in DB through console. But I am not been able to insert
data through my application, don't know what I am doing wrong in that.

Have you specified attr_accessible for the fields?
If, when you do that, then you get a different error then that is the
error that you must address.


No, currently my post.rb file is something like that.


class Post < ActiveRecord::Base

But when I change the file to :

class Post < ActiveRecord::Base
  attr_accessible :title, :content

then on running the application, it is saying,
undefined method `attr_accessible' for #<Class:0x007f3c32758418>

On console =>

NoMethodError (undefined method `attr_accessible' for
  app/models/post.rb:2:in `<class:Post>'
  app/models/post.rb:1:in `<top (required)>'
  app/controllers/posts_controller.rb:29:in `index'

I guess you must be using Rails 4 then. The new way to do this is to
use strong parameters. This gives a simple example of how to do it