Not been able to insert data onto DB

52 views
Skip to first unread message

Sachin Talreja

unread,
Apr 6, 2015, 3:30:30 PM4/6/15
to rubyonra...@googlegroups.com
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

--
Posted via http://www.ruby-forum.com/.

amruby

unread,
Apr 6, 2015, 3:38:27 PM4/6/15
to rubyonra...@googlegroups.com
Did u add these attributes in post.rb as attr_accesble

Sachin Talreja

unread,
Apr 6, 2015, 3:45:46 PM4/6/15
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Apr 6, 2015, 4:52:43 PM4/6/15
to rubyonra...@googlegroups.com
On 6 April 2015 at 20:43, Sachin Talreja <li...@ruby-forum.com> wrote:
> 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

Colin

amtest

unread,
Apr 7, 2015, 1:49:23 AM4/7/15
to rubyonra...@googlegroups.com

Sachin Talreja

unread,
Apr 7, 2015, 10:15:51 AM4/7/15
to rubyonra...@googlegroups.com
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

Sachin Talreja

unread,
Apr 7, 2015, 1:42:26 PM4/7/15
to rubyonra...@googlegroups.com
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.

========================================================
post_contoller.rb file :

class PostsController < ApplicationController
def show
@post=Post.find(params[:id])
end

def new
@post=Post.new
end

def create
@post=Post.new(params[:id])
if @post.save
redirect_to posts_path, :notice => "your post has been inserted"
else
render "new"
end

end

def index
@post=Post.all
end
end
==================================================
index.html.erb file

<h1>My blog</h1>
<% @post.each do |post| %>
<h2><%= link_to post.title , post %></h2>
<p><%= post.content %></p>
<% end %>
<p><%= link_to "Add new post", new_post_path %> </p>
======================================================
new.html.erb file :

<h1>Add a new post</h1>
<p><%= form_for @post do |f| %></p>
<p>

<%= f.label :title %> <br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :content %> <br/>
<%= f.text_area :content %>
</p>
<p>
<%= f.submit "Add a new post" %> <br/>
</p>
<% end %>

========================================================
Console Output:

Started POST "/posts" for 127.0.0.1 at 2015-04-07 23:05:52 +0530
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"YjmybMkOsVRcHjOXvG1NrkFjuk9TGbu54Cd74mLfWJhOvkxBskWefnwAFq65EZmFxEqVSpbUL1OcvY9DHpVuiw==",
"post"=>{"title"=>"Title1", "content"=>"Content1"}, "commit"=>"Add a new
post"}
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES
(?, ?) [["created_at", "2015-04-07 17:35:52.602692"], ["updated_at",
"2015-04-07 17:35:52.602692"]]
(232.0ms) commit transaction
Redirected to http://localhost:3000/posts
Completed 302 Found in 237ms (ActiveRecord: 232.4ms)

Colin Law

unread,
Apr 7, 2015, 4:28:11 PM4/7/15
to rubyonra...@googlegroups.com
On 7 April 2015 at 18:41, Sachin Talreja <li...@ruby-forum.com> wrote:
> 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.

Colin

Sachin Talreja

unread,
Apr 8, 2015, 12:50:26 AM4/8/15
to rubyonra...@googlegroups.com
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

unread,
Apr 8, 2015, 4:06:00 AM4/8/15
to rubyonra...@googlegroups.com
On 8 April 2015 at 05:49, Sachin Talreja <li...@ruby-forum.com> wrote:
> ...
> 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'

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
https://github.com/rails/strong_parameters

Colin
Reply all
Reply to author
Forward
0 new messages