2
我通過rails命令插入數據時遇到了問題。貝婁你可以看到我的模型和問題。標題甚至顯示爲零,我用標題哈希創建了一個Post的新實例。我知道你可以用不同的方式做到這一點。我正在使用這個簡單的例子來找出爲什麼我不能從數據庫插入或顯示數據。通過rails命令將數據插入數據庫的問題
型號
category.rb
class Category < ApplicationRecord
attr_accessor :name
has_many :posts
end
post.rb
class Post < ApplicationRecord
attr_accessor :title, :body, :category_id, :author_id
belongs_to :category
end
Rails的Ç
post = Post.new(:title => 'Test')
=> #<Post id: nil, title: nil, body: nil, category_id: nil, author_id: nil, created_at: nil, updated_at: nil>
查看'attr_accessor'(ruby level)與'attr_accessible'(rails level)的區別:https://stackoverflow.com/questions/3136420/difference-between-attr-accessor-and-attr-accessible – MrYoshiji
I當我使用attr_accessible時得到這個錯誤 未定義的方法'attr_accessible'爲#你的意思是? attr_accessor –
Mahir
看到第一個回答的評論,因爲他們解釋了爲什麼它不適用於Rails 4+ – MrYoshiji