hjds
hjds

Reputation: 63

Rails getting error when trying to upload images with paperclip

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

Upvotes: 1

Views: 485

Answers (2)

Hizqeel
Hizqeel

Reputation: 947

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

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

Upvotes: 1

Rockwell Rice
Rockwell Rice

Reputation: 3002

In your article model you have

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

So it is looking to validate "attachment", not "image". Change the validates line to this

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

and it should work.

Upvotes: 0

Related Questions