【问题标题】:Rails getting error when trying to upload images with paperclip尝试使用回形针上传图像时,Rails 出现错误
【发布时间】:2017-01-01 00:51:05
【问题描述】:

我才开始学习 rails 并不断遇到错误,我主要通过搜索来修复这些错误。我按照http://guides.rubyonrails.org/getting_started.html 上的教程完成了它,现在正在尝试上传图片以获取博客文章。但现在我被一个听起来很简单的事情所困扰,使用回形针上传图像。 我收到如下错误:

#的未定义方法`attachment'

这是我的文件:

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 < ApplicationRecord

 has_attached_file :image, styles: { medium: "300x300>", 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

<%= form_for @article, html: { multipart: true } do |f| %>

<% if @article.errors.any? %>
    <div id="error_explanation">
  <h2>
    <%= pluralize(@article.errors.count, "error") %> prohibited
    this article from being saved:
  </h2>
  <ul>
    <% @article.errors.full_messages.each do |msg| %>
    <li>
      <%= msg %>
    </li>
    <% end %>
  </ul>
</div>
<% end %>

  <p>
  <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

<p>
  <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

  <p>
  <%= f.label :image %><br>
    <%= f.file_field :image %>
  </p>


<p>
  <%= f.submit %>
</p>

<% end %>

【问题讨论】:

  • 您使用的是哪个版本的 Rails?你的article.rb 模型中不是Validates :image 吗?

标签: ruby-on-rails ruby paperclip


【解决方案1】:

在您的 article.rb 模型中,只需将 :attachment 更改为 :image 即可:

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

【讨论】:

  • 现在我收到此错误 - 文件存在 @ sys_fail2 - C:/Users/name/AppData/Local/Temp/8b36e9207c24c76e6719268e49201d9420170101-40796-1m76tw4.png
【解决方案2】:

在你的文章模型中你有

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

所以它正在寻找验证“附件”,而不是“图像”。将 validates 行更改为此

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

它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多