0
这里是我的控制器无法在导轨4上使用回形针连接照片?
class ArticlesController < ApplicationController
def new
@article=Article.new
end
def index
@articles = Article.all
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def show
@article = Article.find(params[:id])
end
def edit
@article = Article.find(params[:id])
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)
end
end
这里是我的形式
<%= 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.file_field :photo %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
我不能上传照片INFACT它不显示任何错误,但是没有任何东西(图像的路径)保存在数据库也没有保存任何照片。我是新手,只是想创建一个具有上传照片功能的表单。
class Article < ActiveRecord::Base
validates :title, presence: true, length: { minimum: 5 }
has_attached_file :photo
validates_attachment :photo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] }
end
我已经试过几乎所有的教程,并也发现了一些Similar Problem的,但似乎一切都没有解决这个问题。请帮助我。
是的我早些时候尝试过,但显示错误,所以我删除了它。错误在ArticlesController#update中显示“Paperclip :: Errors :: MissingRequiredValidatorError”。我也提出过这个错误。在我的模型中添加了“validates_attachment:photo,content_type:{content_type:[”image/jpg“,”image/jpeg“,”image/png“]} ”。但我现在不知道该怎么做。 – Pranav
您是否在模型中的强参数**和** validates_attachment中使用了'photo'?你需要两个;强大的参数将照片传递给模型,并验证附件,因为Paperclip坚持至少验证内容类型或文件名。 –
给我看。我以某种方式删除了Paperclip错误,这真是一个愚蠢的错误。但现在,我正在上传.jpg扩展图像文件。它显示文章页面中的错误为“照片的扩展名与其内容不匹配”。 – Pranav