简体   繁体   中英

Creating records in rails console

I am new with ruby on rails and I am following Ruby on Rails 3 Essential Training 2010 from Lynda.

I have a problem with creating records in rails console even though I create the subject it returns nil. This is the code from the console:

vane@vane-HP-G70-Notebook-PC:~/Sites/simple_cms$ rails c

Loading development environment (Rails 3.2.8)

1.9.3-p194 :001 > subject = Subject.new(:name => "Fourth Subject", :position => 4, :visible => true)

=> Subject id: nil, name: nil, position: nil, visible: false, created_at: nil, updated_at: nil>

1.9.3-p194 :002 > subject.save (0.1ms) BEGIN SQL (0.4ms) INSERT INTO subjects ( created_at , name , position , updated_at , visible ) VALUES ('2012-10-10 15:06:11', NULL, NULL, '2012-10-10 15:06:11', 0) (56.4ms) COMMIT

=> true

It doesn't show any error so I don't now what to do. Any help will be appreciated.

This is what i have in app>models subject.rb:

    class Subject < ActiveRecord::Base 
    attr_accessor :name, :position, :visible 
    attr_accessible :name, :position, :visible
    end

您可能需要使属性可访问,以便可以在new呼叫中分配它们。

After i removed this line

attr_accessor :name, :position, :visible

from subject.rb the problem got solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM