简体   繁体   English

无法使用回形针在导轨4上附加照片?

[英]Cannot Attach photo using paperclip on rails 4?

Here is my controller 这是我的控制器

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

Here's my form 这是我的表格

<%= 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 %>

I am not able to upload the photo infact it does not show any error, but neither anything(path of image) is saved on database nor any photo is saved. 我无法上传照片,实际上它没有显示任何错误,但是没有任何内容(图像路径)保存在数据库中,也没有保存任何照片。 I am new on rails, just trying to create a form which have features of uploading photo. 我是新手,只是尝试创建一个具有上传照片功能的表单。

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

I have tried almost every tutorial and also found some of the Similar Problem but it seems nothing has resolved the issue. 我已经尝试了几乎所有教程,也发现了一些类似问题,但是似乎没有任何问题可以解决。 Please help me out. 请帮帮我。

You need to add photo to your strong params: 您需要将photo添加到强大的参数中:

def article_params
    params.require(:article).permit(:title, :text, :photo)
end

Without it the value is not passed over to the model to be validated and saved. 没有它,该值将不会传递到要验证和保存的模型。

I am assuming you already ran the migration to add Paperclip's photo_file_name , photo_file_size , photo_content_type , photo_updated_at fields to your articles table. 我假设您已经运行了迁移,以将Paperclip的photo_file_namephoto_file_sizephoto_content_typephoto_updated_at字段添加到您的articles表中。

Note: It is only necessary to include the attached file name photo in the strong parameters; 注意:仅需在强参数中包含附件文件名的photo as long as this value reaches the model Paperclip will handle the rest. 只要该值达到模型,回形针将处理其余部分。

In addition, you need to disable Paperclip's spoofing validation. 此外,您需要禁用Paperclip的欺骗验证。 It uses the OS file command the determine the MIME type of the file but Windows doesn't have a file command so it always fails. 它使用OS file命令确定file的MIME类型,但是Windows没有file命令,因此它总是失败。 You can disable the spoofing check by putting something like this in an initializer: 您可以通过在初始化程序中放置以下内容来禁用欺骗检查:

module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM