Ruby on rails 未定义的方法'image';对于#<;邮政编码:0x007f6018a697b0>;
我的项目中出现以下错误: 未定义的方法“image”# 这是我的模型post.rbRuby on rails 未定义的方法'image';对于#<;邮政编码:0x007f6018a697b0>;,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我的项目中出现以下错误: 未定义的方法“image”# 这是我的模型post.rb class Post < ApplicationRecord class Post < ActiveRecord::Base validates :image, presence: true has_attached_file :image, styles: { :medium => "640x" } validates_attachme
class Post < ApplicationRecord
class Post < ActiveRecord::Base
validates :image, presence: true
has_attached_file :image, styles: { :medium => "640x" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
end
class Post“640x”}
验证\u附件\u内容\u类型:图像,:内容\u类型=>/\Aimage\/.\Z/
结束
结束
这是我的控制器post_controller.rb
class PostsController < ApplicationController
def index
end
def new
@post = Post.new
end
end
class PostsController
为什么要使用类Post
新生成的应用程序具有app/models/application\u record.rb
默认显示。新生成的模型必须继承自ApplicationRecord
,但前提是它存在
因此,如果您使用的是rails>=5
,请删除这个类classpost
,或者如果您需要它,请删除第一个
见此参考
未定义的方法“image”
对于模型中找不到的图像
属性,请确保已添加该属性,如果尚未添加该属性,请编写如下命令
rails g migration AddImageToPosts image:string
然后运行
rakedb:migrate
您可以添加与posts相关的schema.rb
部分吗?class Post
——这里发生了什么事??!!!这在我看来绝对是错的。
rails g migration AddImageToPosts image:string