本文介绍了尝试使用回形针上传图像时,Rails 出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

未定义的方法`attachment' for #

这是我的文件:

articles_controller.rb

class ArticlesController 

article.rb

类文章", thumb: "100x100>"}, default_url: "/images/:style/missing.png"验证 :attachment, :attachment_content_type =>{:content_type =>['图片/png', '图片/jpg']}attr_accessor :image_file_namehas_many :comments, 依赖: :destroy验证:标题,存在:真实,长度:{ 最小值:2}结尾

_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") %>禁止这篇文章被保存:<ul><% @article.errors.full_messages.each 做 |msg|%><li><%=味精%><%结束%>

<%结束%><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><%结束%>

解决方案

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

验证 :image, :attachment_content_type =>{:content_type =>['图片/png', '图片/jpg']}

I only started learning rails and keep running into errors that I mostly fix by searching them up. I followed the tutorial on http://guides.rubyonrails.org/getting_started.html and finished it and am now trying to upload images to got with the blog articles. But now I'm stuck with what sounds like a really simple thing to solve, uploading images using paperclip.I get errors like :

undefined method `attachment' for #

Here are my files:

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 %>
解决方案

In your article.rb model just change :attachment to :image like:

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

这篇关于尝试使用回形针上传图像时,Rails 出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 10:47