2016-12-31 3 views
1

Я только начал изучать рельсы и постоянно сталкивался с ошибками, которые я в основном исправляю, просматривая их. Я последовал за учебником по адресу http://guides.rubyonrails.org/getting_started.html и закончил его, и теперь я пытаюсь загрузить изображения, чтобы получить статьи в блоге. Но теперь я застрял в том, что звучит как очень простая вещь для решения, загружая изображения с помощью paperclip. я получаю ошибки типа:Rails получает ошибку при попытке загрузить изображения с помощью paperclip

неопределенный метод `вложения» для #

Вот мои файлы:

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

Какие рельсы версии вы используете? Разве это не 'Validates: image' в вашей модели' article.rb'? – Hizqeel

ответ

1

В вашей article.rb модели просто изменить :attachment к :image как:

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

Спасибо !!!!!! – hjds

+0

Теперь я получаю эту ошибку - Файл существует @ sys_fail2 - C: /Users/name/AppData/Local/Temp/8b36e9207c24c76e6719268e49201d9420170101-40796-1m76tw4.png – hjds

0

В вашей модели статьи у вас есть

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

Так он ищет, чтобы подтвердить "вложение", а не "образ". Измените строку проверки на это:

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

и он должен работать.

Смежные вопросы