2013-02-24 55 views
6

मैं multiple file upload समर्थन करने के लिए carrierwave साथ uploadify के लिए कोशिश कर रहा था, लेकिन यह मुझे इस त्रुटि GET http://localhost:3000/users/uploadify.swf?preventswfcaching=1361694618739 दे रहा है। मूल रूप से मैं model को user नाम दिया गया है। सिंगल अपलोड के लिए यह कैरियरवेव के साथ ठीक काम कर रहा है लेकिन कई फाइलों के लिए यह नहीं है।अपलोडिंग त्रुटि carrierwave का उपयोग करने और uploadify

मैंने this ट्यूटोरियल का पालन किया है। 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> 

मैं mongoid का उपयोग कर रहा हूँ तो मॉडल इस

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

    mount_uploader :image, ImageUploader 
end 

मैं नहीं मिल रहा हूँ त्रुटि है क्या की तरह है। कृपया मेरी मदद करें।

+0

आपने केवल जीईटी-अनुरोध पोस्ट किया है, वास्तविक त्रुटि नहीं। कृपया त्रुटि पोस्ट करें। – Jesper

+0

यह त्रुटि है जो मैं जेएस कंसोल में प्राप्त कर रहा हूं। असल में फाइल बटन ब्राउज़ करना काम नहीं कर रहा है। –

+0

कृपया समझें कि 'http: // localhost: 3000/users/uploadify.swf प्राप्त करें? Preventswfcaching = 1361694618739' कोई त्रुटि नहीं है, यह केवल एक कथन है। कृपया हमें वास्तविक त्रुटि दें। – Jesper

उत्तर

1

कैरियरवेव के साथ कई छवि अपलोड के लिए यहां एक संपूर्ण समाधान है: इन चरणों का पालन करने के लिए।

rails new multiple_image_upload_carrierwave 

मणि फ़ाइल में

gem 'carrierwave' 

तो निम्नलिखित

bundle install 
rails generate uploader Avatar 

बनाएं पद पाड़

rails generate scaffold post title:string 

चलाने post_attachment पाड़ बनाएं

rails generate scaffold post_attachment post_id:integer avatar:string 

फिर

rake db:migrate 

चलाने post.rb

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

में post_attachment.rb

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

में 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 

विचारों/पदों/_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 %> 

किसी भी पद के लिए एक अनुलग्नक और लगाव की सूची को संपादित करने के लिए।/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 %> 

संशोधित अद्यतन post_attachment_controller.rb में विधि विचारों/पदों/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 %> 

अद्यतन प्रपत्र एक लगाव देखा गया संपादित करने के लिए

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 

रेल में 3 मजबूत पैरामीटर को परिभाषित करने की कोई आवश्यकता नहीं है और आप मॉडल में attribute_accessible को परिभाषित कर सकते हैं और मॉडल पोस्ट करने के लिए accept_nested_attribute को परिभाषित कर सकते हैं क्योंकि एक्सेस करने योग्य विशेषता को बहिष्कृत किया गया है रेल 4।

एक अनुलग्नक संपादित करने के लिए हम एक समय में सभी अनुलग्नकों को संशोधित नहीं कर सकते हैं। इसलिए हम संलग्नक को एक-एक करके प्रतिस्थापित करेंगे, या आप अपने नियम के अनुसार संशोधित कर सकते हैं, यहां मैं आपको दिखाता हूं कि कोई अनुलग्नक कैसे अपडेट करें।

+1

देर से बदलना चाहिए, लेकिन आपके उत्तर के लिए धन्यवाद !!! –

+0

क्या यह उत्तर आपकी समस्या का समाधान करता है, या आपको अधिक जानकारी चाहिए? –

+0

चिह्नित करने के लिए भूल गए यह –