1

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 %>
1
  • Which rails version are you using? Isn't it Validates :image in your article.rb model ? Commented Dec 31, 2016 at 16:46

2 Answers 2

1

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

validates :image, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']}
Sign up to request clarification or add additional context in comments.

1 Comment

Now I'm getting this error - File exists @ sys_fail2 - C:/Users/name/AppData/Local/Temp/8b36e9207c24c76e6719268e49201d9420170101-40796-1m76tw4.png
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.