2014-10-10 135 views
0

多张照片,我想多张图片中的Rails 4Rails的4回形针

中的图片不会被上传到数据库中添加使用回形针创业板的项目。使用MySQL。被保存

2个错误禁止这个论坛主题:

我不断收到此错误

照片照片内容类型无效

照片的照片无效

个请求参数:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"zoMHhVTJOy0/VVGNCcVrTaFwmSRvYoonQEtc+TAqwCM=", "project"=>{"photos_attributes"=>{"0"=>{"photo"=>#<ActionDispatch::Http::UploadedFile:0x007f98f0d4b468 @tempfile=#<Tempfile:/var/folders/gp/jz4_vlrs2l96__g4jrnsc3y40000gn/T/RackMultipart20141010-4840-1gtf4ao>, @original_filename="glyphicons-halflings-white.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"project[photos_attributes][0][photo]\"; filename=\"glyphicons-halflings-white.png\"\r\nContent-Type: image/png\r\n">}}, "title"=>"sadfsadfdsa", "description"=>"sdfsdafsdf"}, "commit"=>"Create Project"} 

我有如下的添加必要的文件。

**新建项目厄尔布/ HTML文件**

<% provide(:title, "New Project") %> 

<div class="page-header"> 
<h2>New Project</h2> 
</div> 

<%= form_for @project, :html => { :multipart => true } do |f| %> 
<% if @project.errors.any? %> 
<div id="error_explanation"> 
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this forum topic from being saved: </h2> 
    <ul> 
    <% @project.errors.full_messages.each do |msg| %> 
    <li><%= msg %></li> 
    <% end %> 
    </ul> 
</div> 
<%= f.fields_for :photos do |f| %> 
    <div class="form-group"> 
    <%= f.file_field :photo , class: 'form-group' %> 
    </div> 
<% end %> 

<div class="form-group"> 
<%= f.text_area :title, class: 'form-control input-lg', placeholder: 'Topic Title' %> 
</div> 
<div class="form-group"> 
<%= f.text_area :description, rows: 4, class: 'form-control', placeholder: 'Topic Description' %> 
</div> 

<div class="actions"> 
<%= f.submit class: 'btn btn-success'%> 
</div> 
<% end %> 

项目控制器:

class ProjectsController < ApplicationController 
    before_filter :authenticate_user!, except: [:show, :index] 
    before_filter :set_project, except: [:index, :new, :create] 

def index 
@projects = Project.find(:all) 
end 

def show 
end 

def new 
@project = Project.new 
3.times {@project.photos.build} 
end 

def create 
@project = current_user.projects.build(project_params) 
    if @project.save 
    flash[:success] = "Project Created!" 
    redirect_to @project 
    else 
    render :new 
    end 
end 

def edit 
3.times {@project.photos.build} 
end 

def update 
if @project.update_attributes(project_params) 
    flash[:notice] = "Successfully updated your project." 
    redirect_to @project 
else 
    render :edit 
    end 
end 

def destroy 
end 

private 

# Never trust parameters from the scary internet, only allow the white list through. 
def project_params 
params.require(:project).permit(:title, :description, photos: [], photos_attributes:[:photo, :id]) 
end 

def set_project 
@project = Project.find(params[:id]) 
end 

项目型号:

class Project < ActiveRecord::Base 
    belongs_to :user, counter_cache: true 
    has_many :photos 

    default_scope -> { order('created_at DESC') } 
    validates :title, presence: true, length: {maximum: 50} 
    validates :description, presence: true, length: { maximum: 1000 } 
    validates :user_id, presence: true 

    accepts_nested_attributes_for :photos, :reject_if => lambda { |t| t[:photo].nil? } 

    def photos=(files = []) 
    files.each{|f| (@images ||= []) << photos.create(image: f) } 
    end 
end 

照片型号:

class Photo < ActiveRecord::Base 
belongs_to :user, counter_cache: true 
belongs_to :project, counter_cache: true 

has_attached_file :photo, :styles => { :small => "150x150>", :large => "320x240>" } 
validates_attachment_presence :photo 
validates_attachment_size :photo, :less_than => 5.megabytes 
validates_attachment :photo, content_type: { content_type: ["photo/jpg", "photo/jpeg", "photo/png", "photo/gif"] } 
end 

照片迁移:

class AddPhotosToPhotos < ActiveRecord::Migration 
def self.up 
    add_column :photos, :photo_file_name, :string 
    add_column :photos, :photo_content_type, :string 
    add_column :photos, :photo_file_size, :integer 
    add_column :photos, :photo_updated_at, :datetime 
end 

def self.down 
remove_column :photos, :photo_file_name 
remove_column :photos, :photo_content_type 
remove_column :photos, :photo_file_size 
remove_column :photos, :photo_updated_at 
end 
end 

照片移民指标

class CreatePhotos < ActiveRecord::Migration 
def change 
    create_table :photos do |t| 
    t.integer :user_id 
    t.integer :project_id 
    t.timestamps 
    end 
    end 
end 

Development.rb

Paperclip.options[:command_path] = "/usr/local/bin/" 

回答

0

首先,内容类型的错误 - 改变你的validates_attachment图像类型:

validates_attachment :photo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] } 

至于其他错误,它看起来像它的出现在这里:

<%= f.fields_for :photos do |f| %> 
    <div class="form-group"> 
     <%= f.file_field :photo , class: 'form-group' %> 
    </div> 
    <% end %> 

在复数个fields_for中可能存在单数照片的问题。诚然,我没有检查过文件。如果不是,将需要更多详细的错误信息来排除故障。当您在创建操作中调用project_params时会发生什么?

+0

修复内容验证的好机会会照顾另一个错误,让我们希望这一点。 – bjorn 2014-10-11 05:04:06

+0

感谢您的回答! fields_for对“Photo”和validates_attachment的更改不起作用。我相信内容类型变为“图像”使我走上了正轨,但问题正在代码的其他地方发生。 – 2014-10-11 17:37:05