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 8099623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:28:19+00:00 2026-06-05T22:28:19+00:00

Today i have a little problem containing a File Upload. First some Infos: Rubyversion:

  • 0

Today i have a little problem containing a File Upload.

First some Infos:

Rubyversion: 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
Railsversion: Rails 3.1.1

Needed Code:
My Form

        <%= form_for @label, :html => { :multipart => true } do |f| %>
            <% if @label.errors.any? %>
                    <div id="error_explanation">
                            <h2><%= pluralize(@label.errors.count, "error") %> prohibited this label from being saved:</h2>

                            <ul>
                            <% @label.errors.full_messages.each do |msg| %>
                                    <li><%= msg %></li>
                            <% end %>
                            </ul>
                    </div>
            <% end %>
            <div class="field">
                    <%= f.label :file %><br />
                    <%= f.file_field :file %>
            </div>
            <div class="actions">
                    <%= f.submit %>
            </div>
    <% end %>

My Model:

class Label < ActiveRecord::Base
    attr_accessor :file
    attr_reader :file

    def file=(file)
            log = Logger.new(Rails.root.join("log/label.log").to_s)
            log.info "file-Action called"
            path=file.tempfile.to_path.to_s
            filename="labellist.csv"
            dest=Rails.root.join("app/assets/csv/").join(filename).to_s
            FileUtils.cp(path, dest)
            csv=CsvLabelParser.new
            log.info "New Parser initialized!"
            csv.parse_csv(dest)
            log.info "Variables: path: #{path.inspect} <-----> dest: #{dest.inspect}"
    end
end

My Parser

class CsvLabelParser
    require 'csv'

    def initialize
            @logger=Logger.new(Rails.root.join("log/parser.log").to_s)
    end

    def logger
            @logger
    end

    def parse_csv(path)
            counter = 0
            read_handle = File.open(path, "rb")
            content = read_handle.read
            self.logger.info "CONTENT: #{content.inspect}"
            read_handle.close

            content.each_line do |line|
                    if counter != 0
                            csv_array=line.split(";")
                            self.logger.info "CSV-Array No. #{counter}: #{csv_array.inspect}"
                            label=Label.new

                            label.labnr=csv_array[10]
                            label.name=csv_array[0]
                            label.firm1=csv_array[3]
                            label.firm2=csv_array[4]
                            label.postal=csv_array[7]
                            label.city=csv_array[8]
                            label.country=csv_array[9]
                            label.street=csv_array[5]
                            label.pob=csv_array[6]
                            label.save
                    end
                    counter += 1
            end
    end

end

Controller-Log

# Logfile created on 2012-06-06 09:49:23 +0200 by logger.rb/31641
Controller-create-method called! => variables: Params: {"utf8"=>"✓", "authenticity_token"=>"L+fpIMqFA9qe9U/LxU+atFONT8e3L5xEUum1321mRng=", "label"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x00000003df2318 @original_filename="ADRBW076-Q.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"label[file]\"; filename=\"ADRBW076-Q.csv\"\r\nContent-Type: text/csv\r\n", @tempfile=#<File:/tmp/RackMultipart20120606-2741-1izzofa>>}, "commit"=>"Create Label", "action"=>"create", "controller"=>"labels"}<-----> Label: #<Label id: nil, labnr: nil, name: nil, firm1: nil, firm2: nil, postal: nil, city: nil, country: nil, street: nil, pob: nil, created_at: nil, updated_at: nil>

So! As you can see i want to upload a CSV-file to parse it into my database. A copy of the File should be stored in a folder called CSV for later use. I’ve done that about 5 times before and it worked well, but this time it seems, that the file-method in the model isn’t called.
For now i only want it to work nothing more there will be some changes to catch errors and stuff like that in the future ;).
I’ve logged the Controller action, which is called every time. May the Logfile can Help you! For me it looks okay, comparing it to other logfiles.
I’ve searched for a solution, but everything looks okay to me (maybe i’ve just forgotten a : or a ,), so i can’t really say what’s wrong!

Thanks for your help

  • 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-06-05T22:28:20+00:00Added an answer on June 5, 2026 at 10:28 pm

    I have the Solution!

    I am using Rails 3.1.1 with Ruby 1.9.3 and it’s gemset. The Problem was the new feature “attr_accessible”. Normally not used in Rails 3.1.1 it became necessary to make the :file-Field accessible due to the newer gemset. So the Solution is simple, I just had to add:

    attr_accessible :file, ect
    

    To the Top of my model and everything worked fine!

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

Sidebar

Related Questions

Hi Today I encountered a new problem with MouseWheel I have written some codes
I've run on a little problem today: I have a JS drop down menu
Today I changed mysql to PDO. And now I have this little problem. So
I'm familiar with using NSLocalizedString() to localize strings, but the problem I have today
Hi everyone my problem today is I have a couple of buttons that slide
today i'm having a little problem, that probably is nothing for pros here :)
I ran into a little problem today when I was creating a really quick
I've got a wonderfully fun little SQL problem to solve today and thought I'd
This problem is making me crazy. Actually I have multiple problems. First one: Why
I ran into a little problem today that I can't seem to solve in

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.