尝试使用回形针上传图像时Rails收到错误

我只是开始学习rails并继续遇到我通常通过搜索来解决的错误。 我按照http://guides.rubyonrails.org/getting_started.html上的教程完成了它,现在我正在尝试将图像上传到博客文章中。 但现在我被困在一个听起来非常简单的事情,使用回形针上传图像。 我得到的错误如下:

未定义的方法`附件’为#

这是我的文件:

articles_controller.rb

class ArticlesController < ApplicationController def index @articles = Article.all end def show @article = Article.find(params[:id]) end def new @article = Article.new end def edit @article = Article.find(params[:id]) end def create @article = Article.new(article_params) #@article = Article.create(article_params) if @article.save redirect_to @article else render 'new' end end def update @article = Article.find(params[:id]) if @article.update(article_params) redirect_to @article else render 'edit' end end def destroy @article = Article.find(params[:id]) @article.destroy redirect_to articles_path end private def article_params params.require(:article).permit(:title, :text, :image) end end 

article.rb

 class Article ", thumb: "100x100>" }, default_url: "/images/:style/missing.png" validates :attachment, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']} attr_accessor :image_file_name has_many :comments, dependent: :destroy validates :title, presence: true, length: { minimum: 2 } end 

_form.html.erb

   

prohibited this article from being saved:




在你的article.rb模型中只需更改:attachment :image like:

 validates :image, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']} 

在您的文章模型中

 validates :attachment, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']} 

所以它希望validation“附件”,而不是“图像”。 将validation行更改为此

 validates :image, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']} 

它应该工作。