Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6808505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:57:50+00:00 2026-05-26T19:57:50+00:00

In my project I currently have different models (Project, Message, etc) that: has_many :assets,

  • 0

In my project I currently have different models (Project, Message, etc) that:

has_many :assets, :as => :attachable, :dependent => :destroy

Each Asset is basically a model with a CarrierWave file. Normally I would just use accepted_nested_attributes on the parent model (Project, Message, etc) and have the file upload fields listed in a fields_for block.

My problem is that since I’m using jQuery-File-Uploader with AJAX the parent model’s form will call the parent model’s Create method when ever a file is uploaded. The rest of the parent model fields might not be filled out yet. I’m thinking maybe I could have the file uploader call the create method in the Assets controller, but then I would some how have to send the parent model’s class so the polymorphic association is stored correctly.

Any ideas of how I might get this working cleanly? Thanks for looking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:57:51+00:00Added an answer on May 26, 2026 at 7:57 pm

    OK.

    Step 1

    add gem 'carrier wave' to you Gemfile

    Step 2

    save the code to /lib/flash_session_cookie_middleware.rb

    require 'rack/utils'
    
    class FlashSessionCookieMiddleware
      def initialize(app, session_key = '_session_id')
        @app = app
        @session_key = session_key
      end
    
      def call(env)
        if env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/
          req = Rack::Request.new(env)
          env['HTTP_COOKIE'] = [ @session_key,
                                 req.params[@session_key] ].join('=').freeze unless req.params[@session_key].nil?
          env['HTTP_ACCEPT'] = "#{req.params['_http_accept']}".freeze unless req.params['_http_accept'].nil?
        end
    
        @app.call(env)
      end
    end
    

    Step3

    edit session_store.rb add the code to the end of file

    Rails.application.config.middleware.insert_before(
      ActionDispatch::Session::CookieStore,
      FlashSessionCookieMiddleware,
      Rails.application.config.session_options[:key]
    )
    

    Step4

    Download jquery.uploadify.js from Uploadify and unzip it.

    Step5

    1. Copy jquery.uploadify.v2.1.4.min.js & swfobject.js to
      /app/assets/javascripts if you use Rails3.1 or later, to
      /public/javascripts if you use Rails 3.0 or before version.
    2. Copy uploadify.swf and cancel.png to /app/assets/images/ or
      /public/images
    3. Copy uploadify.css to /app/assets/stylesheets/ or
      /public/stylesheets

    Step6

    Edit your application.js, insert below code to it

    //= require swfobject
    //= require jquery.uploadify
    

    Step7

    In you upload page, add this

    <input id="uploadify" name="uploadify" type="file" />
    

    Step8

    add this code to you upload page

    $(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] %>';
    
      $('#uploadify').uploadify({
        uploader        : '/assets/uploadify.swf',
        script          : '/photos',
        cancelImg       : '/images/cancel.png',
        auto            : true,
        multi           : true,
        removeCompleted : true,
        scriptData      : uploadify_script_data,
        onComplete      : function(event, ID, fileObj, doc, data) {
        }
      });
    });
    

    Step9

    Write your controller like this

    def create
      @photo = Photo.new(:image => params[:Filedata])
      @photo.save
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have a root-level repository set up for each project, like so: Project1
I have a project that I'm currently working on but it currently only supports
I'm working on a project that performs physiological simulations using mathematical models. We currently
I have an existing site that has a bunch of different models and controllers.
Currently the project I'm working with does not have completely fixed models (due to
I have a project that is currently in production delivering some web-services using the
I currently have a recently started project in development with a 2010 target date
I currently have the DatePicker in my project but there are problems due to
I currently have a file abc.htm in my Custom Server Control Project and it's
Currently we have a project with a standard subversion repository layout of: ./trunk ./branches

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.