Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uploading error when using carrierwave and uploadify

i was trying for uploadify with carrierwave to support multiple file upload but it is giving me this error GET http://localhost:3000/users/uploadify.swf?preventswfcaching=1361694618739. Basically i ve one model named as user. For single upload it is working fine with carrierwave but for multiple file it is not.

I ve followed this tutorial. 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 using mongoid so the model is like this

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

   mount_uploader :image, ImageUploader 
 end

I m not getting what is the error. Please help me out.

like image 433
Ahmad hamza Avatar asked Feb 24 '13 08:02

Ahmad hamza


1 Answers

Here's a complete solution for multiple image uploads with Carrierwave: To do just follow these steps.

rails new multiple_image_upload_carrierwave

In gem file

gem 'carrierwave'

Then run the following

bundle install
rails generate uploader Avatar 

Create post scaffold

rails generate scaffold post title:string

Create post_attachment scaffold

rails generate scaffold post_attachment post_id:integer avatar:string

Then run

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 views/posts/_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 %>

To edit an attachment and list of attachment for any post. In views/posts/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 %>

Update form to edit an attachment views/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 %>

Modify update method in 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 no need to define strong parameters and as you can define attribute_accessible in both the model and accept_nested_attribute to post model because attribute accessible is deprecated in rails 4.

For edit an attachment we cant modify all the attachments at a time. so we will replace attachment one by one, or you can modify as per your rule, Here I just show you how to update any attachment.

like image 66
Andrew Hendrie Avatar answered Oct 17 '22 09:10

Andrew Hendrie