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

  • SEARCH
  • Home
  • 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 7397823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:47:47+00:00 2026-05-29T03:47:47+00:00

I am trying to create an upload form that allows the user to select

  • 0

I am trying to create an upload form that allows the user to select a blank value which will then result in another upload process. ( I substituted in “class” for my actual class name).

My upload form is

<%= form_for @upload, :url => class_uploads_path(@uploader), :method =>            
 :post, :html =>{ :multipart => true } do |f| %>
<%= render "shared/error_messages", :target => @upload %>

  <p>This media relates to...<br/>
 <%= select "upload", "class_id", Class.all.collect {|class| [class.name,class.id] }, {        
  :include_blank => true } %></p>

  <p>Title:<br/>
  <%= f.text_field :title %></p>

 <p>Description:<br/>
  <%= f.text_area :description, :rows => 3 %></p>

 <p>File to upload:<br/>
  <%= f.file_field :data %></p>

  <p><%= f.submit 'Upload for review' %></p>
   <% end %>

Here is my create method, if the user leaves the select form blank I would like the first upload action to run and if they chose a class I want the second upload to run.

def create          
  upload_params = params[:upload] || params[:image] || params[:video]
   @class = Class.find(upload_params[:class_id]) || nil

  begin
    if upload_params[:data]
      file_name = upload_params[:data].original_filename
      file_contents = upload_params[:data].read
    end
  rescue
    @upload = Upload.new
    flash[:error] = "Could not find upload file data.  Please reselect file."
    render :action => 'new' and return
  end

  begin
    if @class.nil?
            @upload = Upload.factory({ :file => file_contents, 
                                        :name => file_name, 
                                        :network => @uploader,
                                        :title => upload_params[:title],
                                        :description => upload_params[:description] })
    else
         @upload = Upload.factory({ :file => file_contents, 
                                        :name => file_name, 
                                        :class => @class,
                                        :network => @uploader,
                                        :title => upload_params[:title],
                                        :description => upload_params[:description] })
    end
    @upload.save!

I have tried a number of different hacks, I am trying to avoid adding a new column to the database. Thanks in advance.

  • 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-29T03:47:47+00:00Added an answer on May 29, 2026 at 3:47 am

    Kyle, if the blank option is chosen, I believe params[:class_id] should be “” (a blank string). You should check this — look in your development logs and see what params are getting passed back to your controller method, both when you select a class and when you don’t.

    If that is correct:

    class_id = params[:class_id]
    @class   = Class.find(class_id.to_i) if class_id and not class_id.empty?
    

    As gg_s pointed out, there is lots of room for refactoring the code you posted. But if you just want to get the existing code working, this might be helpful.

    In future, when something is not working, rather than just “hacking on it until it works”, look at the logs/console output for more information. Add logging statements to the controller method, printing out the values of variables, etc. Then try it out, and look at the logs again to see what is happening inside the method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello there i'm trying to create a form with Zend_Form that will enable my
I'm trying to create a form that will handle video file uploads from the
I'm trying to craete a site which allows users to upload any file type
I'm trying to create a simple file upload form for my website. I'm using
Little question: I'm trying to create a Form to upload a file. Now i
I'm trying to create a form that contains these multidimensional arrays: <input type=text name=cost[1][desc]>
I am trying to create a file upload field that has a little bit
I am trying create a WCF service that leverages the WPF MediaPlayer on the
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a user account in a test. But getting a Object reference

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.