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/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/17ceeb0b4066ce4f668d5991a68d821d%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Amruby at Apr 6, 2015 at 7:38 pm
    Did u add these attributes in post.rb as attr_accesble
    On Monday, 6 April 2015 23:30:30 UTC+4, Ruby-Forum.com User wrote:

    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/.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d1fd52a7-09de-4e90-91a4-71c9eef63b23%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sachin Talreja at Apr 6, 2015 at 7:43 pm

    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

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/cd3fcdbf3f54ba2107a5822a484d18c5%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Apr 6, 2015 at 8:52 pm

    On 6 April 2015 at 20:43, Sachin Talreja 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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvPHt68DaKCaOn-xiAG99zpvtjkYg7_65ct%3DF3i-EDPPw%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Amtest at Apr 7, 2015 at 5:49 am
    attr_accessible, not attr_accessor, that @colin Law mentioned.

    http://stackoverflow.com/questions/3136420/difference-between-attr-accessor-and-attr-accessible
    On Monday, 6 April 2015 23:45:46 UTC+4, Ruby-Forum.com User 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
    end

    --
    Posted via http://www.ruby-forum.com/.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5b6e63b8-f2a7-4631-827e-73338120d8a9%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sachin Talreja at Apr 7, 2015 at 2:15 pm
    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

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/28eeb22d42eff57663eeb7a7f650f534%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sachin Talreja at Apr 7, 2015 at 5:41 pm
    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)

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/443f20012ceb14f6e98a44e6d8ef6c79%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Apr 7, 2015 at 8:27 pm

    On 7 April 2015 at 18:41, Sachin Talreja 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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsC0xxdM8%2BrM%2BGBy6LBM5u%3DQgZ7Ra5BjMT5fReV5JOFqQ%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sachin Talreja at Apr 8, 2015 at 4:49 am
    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'

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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f3da5537581ac2c6abbafeca602d9209%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Apr 8, 2015 at 8:05 am

    On 8 April 2015 at 05:49, Sachin Talreja 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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLs%3D_rkTNddVLcYAGT1Qk0YoiD1TfpE4B%3D5F0Pv%2B7PxY6Q%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 6, '15 at 7:30p
activeApr 8, '15 at 8:05a
posts10
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase