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
end

end
end

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
end

On 6 April 2015 at 20:43, Sachin T. [email protected] 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

attr_accessible, not attr_accessor, that @colin Law mentioned.

http://stackoverflow.com/questions/3136420/difference-between-attr-accessor-and-attr-accessible

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

On 7 April 2015 at 18:41, Sachin T. [email protected] 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

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

My blog

<% @post.each do |post| %>

<%= link_to post.title , post %>

<%= post.content %>

<% end %>

<%= link_to "Add new post", new_post_path %>

====================================================== new.html.erb file :

Add a new post

<%= form_for @post do |f| %>

<%= f.label :title %>

<%= f.text_field :title %>

<%= f.label :content %>
<%= f.text_area :content %>

<%= f.submit "Add a new post" %>

<% 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)

On 8 April 2015 at 05:49, Sachin T. [email protected] wrote:


undefined method attr_accessible' for #<Class:0x007f3c32758418>): app/models/post.rb:2:inclass:Post
app/models/post.rb:1:in <top (required)>' app/controllers/posts_controller.rb:29:inindex’

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

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:inclass:Post
app/models/post.rb:1:in <top (required)>' app/controllers/posts_controller.rb:29:inindex’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs