2014-10-10 130 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

相關問題