Rails 尝试使用回形针上传图片时出现错误
Rails getting error when trying to upload images with paperclip
我才开始学习 rails 并保持 运行 错误,我主要通过搜索来修复这些错误。我遵循了 http://guides.rubyonrails.org/getting_started.html 上的教程并完成了它,现在我正在尝试上传图像以获取博客文章。但是现在我被一个听起来很简单的问题所困扰,那就是使用回形针上传图像。
我收到如下错误:
#
的未定义方法“附件”
这是我的文件:
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 :attachment, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']}
所以它正在寻求验证 "attachment",而不是 "image"。将验证行更改为此
validates :image, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']}
它应该可以工作。
在您的 article.rb
模型中,只需将 :attachment
更改为 :image
,例如:
validates :image, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']}
我才开始学习 rails 并保持 运行 错误,我主要通过搜索来修复这些错误。我遵循了 http://guides.rubyonrails.org/getting_started.html 上的教程并完成了它,现在我正在尝试上传图像以获取博客文章。但是现在我被一个听起来很简单的问题所困扰,那就是使用回形针上传图像。 我收到如下错误:
#
的未定义方法“附件”这是我的文件:
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 :attachment, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']}
所以它正在寻求验证 "attachment",而不是 "image"。将验证行更改为此
validates :image, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']}
它应该可以工作。
在您的 article.rb
模型中,只需将 :attachment
更改为 :image
,例如:
validates :image, :attachment_content_type => { :content_type => ['image/png', 'image/jpg']}