2013-02-24 9 views
6

stavo cercando per uploadify con carrierwave per supportare multiple file upload ma mi sta dando questo errore GET http://localhost:3000/users/uploadify.swf?preventswfcaching=1361694618739. Fondamentalmente ho uno model chiamato come user. Per il caricamento singolo funziona bene con carrierwave ma per file multipli non lo è.caricamento errore quando si utilizza carrierwave e uploadify

Ho seguito il tutorial this. In users/_form.rb

<p> 
<%= f.label "Upload Images"%> 
<%= f.file_field :image, :multiple => true %> 
</p> 

<script type= "text/javascript"> 
$(document).ready(function() { 
<% key = Rails.application.config.session_options[:key] %> 
    var uploadify_script_data = {}; 
    var csrf_param = $('meta[name=csrf-param]').attr('content'); 
    var csrf_token = $('meta[name=csrf-token]').attr('content'); 
    uploadify_script_data[csrf_param] = encodeURI(encodeURIComponent(csrf_token)); 
    uploadify_script_data['<%= key %>'] = '<%= cookies[key] %>'; 

    $('#user_image').uploadify({ 
    uploader  : '<%= asset_path("uploadify.swf")%>', 
    script   : '/images', 
    cancelImg  : '<%= asset_path("uploadify-cancel.png")%>', 
    auto   : true, 
    multi   : true, 
    removeCompleted  : true, 
    scriptData  : uploadify_script_data, 
    onComplete  : function(event, ID, fileObj, doc, data) { 
    } 
    }); 
}); 
</script> 

I m utilizzando mongoid modo che il modello è come questo

class User 
include Mongoid::Document 
field :name, type: String 
field :description, type: String 
field :image, type: String 

    mount_uploader :image, ImageUploader 
end 

io non sto ottenendo quello che è l'errore. Per favore aiutatemi.

+0

Hai appena inviato la richiesta GET, non l'errore effettivo. Si prega di inviare l'errore. – Jesper

+0

è l'errore che sto ottenendo nella console js. In realtà il pulsante Sfoglia i file non funziona. –

+0

Si prega di capire che 'OTTIENI http: // localhost: 3000/users/uploadify.swf? Avoidwfcaching = 1361694618739' non è un errore, è semplicemente una dichiarazione. Si prega di darci l'errore reale. – Jesper

risposta

1

Ecco una soluzione completa per più caricamenti di immagini con Carrierwave: Per fare solo seguire questi passaggi.

rails new multiple_image_upload_carrierwave 

Nel file di gemma

gem 'carrierwave' 

quindi eseguire il seguente

bundle install 
rails generate uploader Avatar 

Crea messaggio impalcatura

rails generate scaffold post title:string 

Crea impalcatura post_attachment

rails generate scaffold post_attachment post_id:integer avatar:string 

Quindi eseguite

rake db:migrate 

In post.rb

class Post < ActiveRecord::Base 
    has_many :post_attachments 
    accepts_nested_attributes_for :post_attachments 
end 

In post_attachment.rb

class PostAttachment < ActiveRecord::Base 
    mount_uploader :avatar, AvatarUploader 
    belongs_to :post 
end 

In post_controller.rb

def show 
    @post_attachments = @post.post_attachments.all 
end 

def new 
    @post = Post.new 
    @post_attachment = @post.post_attachments.build 
end 

def create 
    @post = Post.new(post_params) 

    respond_to do |format| 
    if @post.save 
     params[:post_attachments]['avatar'].each do |a| 
     @post_attachment = @post.post_attachments.create!(:avatar => a, :post_id => @post.id) 
     end 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
    else 
     format.html { render action: 'new' } 
    end 
    end 
end 

private 
    def post_params 
     params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar]) 
    end 

In vista/messaggi/_form.html.erb

<%= form_for(@post, :html => { :multipart => true }) do |f| %> 
    <div class="field"> 
    <%= f.label :title %><br> 
    <%= f.text_field :title %> 
    </div> 

    <%= f.fields_for :post_attachments do |p| %> 
    <div class="field"> 
     <%= p.label :avatar %><br> 
     <%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %> 
    </div> 
    <% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

Per modificare un allegato e la lista di attacco per ogni messaggio. In vista/messaggi/show.html.erb

<p id="notice"><%= notice %></p> 

<p> 
    <strong>Title:</strong> 
    <%= @post.title %> 
</p> 

<% @post_attachments.each do |p| %> 
    <%= image_tag p.avatar_url %> 
    <%= link_to "Edit Attachment", edit_post_attachment_path(p) %> 
<% end %> 

<%= link_to 'Edit', edit_post_path(@post) %> | 
<%= link_to 'Back', posts_path %> 

forma Update per modificare una vista di attacco/post_attachments/_form.html.erb

<%= image_tag @post_attachment.avatar %> 
<%= form_for(@post_attachment) do |f| %> 
    <div class="field"> 
    <%= f.label :avatar %><br> 
    <%= f.file_field :avatar %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

metodo di aggiornamento Modifica nella post_attachment_controller.rb

def update 
    respond_to do |format| 
    if @post_attachment.update(post_attachment_params) 
     format.html { redirect_to @post_attachment.post, notice: 'Post attachment was successfully updated.' } 
    end 
    end 
end 

In rails 3 non è necessario definire parametri forti e come è possibile definire attributo_accessibile sia nel modello che accept_nested_attribute per postare il modello perché l'attributo accessibile è deprecato in rotaie 4.

Per modificare un allegato non è possibile modificare tutti gli allegati alla volta. quindi sostituiremo uno per uno gli allegati, oppure puoi modificare secondo la tua regola, qui ti mostrerò come aggiornare qualsiasi allegato.

+1

In ritardo ma grazie per la tua risposta !!! –

+0

La risposta ha risolto il tuo problema o hai bisogno di ulteriori informazioni? –

+0

Hai dimenticato di segnare it –