Я только начал изучать рельсы и постоянно сталкивался с ошибками, которые я в основном исправляю, просматривая их. Я последовал за учебником по адресу 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 %>
Какие рельсы версии вы используете? Разве это не 'Validates: image' в вашей модели' article.rb'? – Hizqeel