11155
(-- --)
April 6, 2015, 7:29pm
1
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
end
end
end
amruby
(amruby)
April 6, 2015, 7:38pm
2
Did u add these attributes in post.rb as attr_accesble
11155
(-- --)
April 6, 2015, 7:43pm
3
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
end
Colin_Law
(Colin Law)
April 6, 2015, 8:51pm
4
attr_accessible is not the same as attr_accessor
Colin
amruby
(amruby)
April 7, 2015, 5:49am
5
attr_accessible, not attr_accessor, that @colin Law mentioned.
ruby-on-rails, ruby
11155
(-- --)
April 7, 2015, 2:15pm
6
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
end
11155
(-- --)
April 7, 2015, 5:41pm
7
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.
Colin_Law
(Colin Law)
April 7, 2015, 8:26pm
8
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.
Colin
11155
(-- --)
April 8, 2015, 4:49am
9
No, currently my post.rb file is something like that.
post.rb
class Post < ActiveRecord::Base
end
But when I change the file to :
class Post < ActiveRecord::Base
attr_accessible :title, :content
end
then on running the application, it is saying,
undefined method `attr_accessible' for #<Class:0x007f3c32758418>
On console =>
NoMethodError (undefined method `attr_accessible' for
#<Class:0x007f3c32758418>):
app/models/post.rb:2:in `<class:Post>'
app/models/post.rb:1:in `<top (required)>'
app/controllers/posts_controller.rb:29:in `index'
Colin_Law
(Colin Law)
April 8, 2015, 8:04am
10
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
Colin