Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 无法使用回形针在导轨4上粘贴照片?_Ruby On Rails_Ruby_Ruby On Rails 4_Paperclip - Fatal编程技术网

Ruby on rails 无法使用回形针在导轨4上粘贴照片?

Ruby on rails 无法使用回形针在导轨4上粘贴照片?,ruby-on-rails,ruby,ruby-on-rails-4,paperclip,Ruby On Rails,Ruby,Ruby On Rails 4,Paperclip,这是我的控制器 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 el

这是我的控制器

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
class-ArticlesController
这是我的表格

<%= 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 %>
{:multipart=>true}do | f |%>
禁止保存此文章:


我无法上传照片,因为它没有显示任何错误,但数据库中既没有保存任何内容(图像路径),也没有保存任何照片。我是rails新手,只是想创建一个具有上传照片功能的表单

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
类文章

我尝试了几乎每一个教程,也发现了一些问题,但似乎没有解决这个问题。请帮帮我

您需要将
照片
添加到您的强参数中:

def article_params
    params.require(:article).permit(:title, :text, :photo)
end
没有它,值不会传递给要验证和保存的模型

我假设您已经运行了迁移,将曲别针的
照片文件名
照片文件大小
照片内容类型
照片更新位置
字段添加到您的
文章
表中

注:只需在强参数中包含所附文件名
photo
;只要此值达到模型,回形针将处理其余的

此外,您需要禁用曲别针的欺骗验证。它使用OS
file
命令来确定文件的MIME类型,但Windows没有file命令,因此它总是失败。您可以通过在初始值设定项中放入类似的内容来禁用欺骗检查:

module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

是的,我之前试过了,但出现了一个错误,所以我删除了它。错误显示“曲别针::错误::缺少ArticlesController#更新中的RequiredValidator错误”。我以前也犯过这个错误。这时,我在我的模型中添加了“validates_attachment:photo,content_type:{content_type:[“image/jpg”,“image/jpeg”,“image/png”]}”。但我不知道现在该怎么办。您是否尝试过在强参数中使用
photo
,并在模型中使用
验证附件
?你两者都需要;strong参数将照片传递给模型,并验证_附件,因为曲别针坚持要求您至少验证内容类型或文件名。Parden me。我不知何故消除了那个回形针的错误,那个是愚蠢的错误。但现在我正在上传一个.jpg扩展名图像文件。它在文章页面中显示错误为“照片的扩展名与其内容不匹配”。