Forum: Ruby on Rails Dynamic file paths in FileColumn

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eee6ef9a468897f0a5123095ae697a81?d=identicon&s=25 Aaron Worsham (aaronworsham)
on 2006-06-10 23:21
I have a file uploader application in ROR using the very nice FileColumn
plugin.  In looking at the source code I can see that there is a
DEFAULT_OPTION hash with some root path options and such which can be
merged from an options paramater.  These static path changes are not a
problem.

My question is dynamic path options.  My upload form, when you boil it
down, has two fields.  The first is the 'file_column_field' form helper
which grabs the file off the harddrive and adds its filename to a :path
attribute of the Model.  The second is a drop down which selects from a
list of publications that the file belongs to.  This gets written to the
:publication attribute in the Model.  Id like to pass the :publication
value to FileColumn so that is can use it in the path.  Being a rather
poor hobby coder, I am sure that I am missing something very basic.

Here is what I have.  When I run it I get the error 'Cannot convert
Symbol to String'

new.rhtml
 <%= form_tag ( { :action => "post" }, { :multipart => true })%>
 Publication:
 <%= collection_select(:xml, :publication_id, @publications, :id,
:name)%>
 XML File:
 <%= file_column_field "xml", "path" %>
 <%= submit_tag(value = "Create new article") %>
 <%= end_form_tag %>

class UploadController < ApplicationController
    @xml = Xml.new(@params[:xml])
    @xml.save
end

class Xml < ActiveRecord::Base
    file_column :path, {:pub => :publication_id}
end

#small change to FileColumn (appologies to Mr. Kanthak)
module FileColumn # :nodoc:
...
  def self.init_options(defaults, attr)
    options = defaults.dup
    options[:store_dir] ||= File.join(options[:root_path],
options[:pub])
    options[:tmp_base_dir] ||= File.join(options[:store_dir], "tmp")
    options[:base_url] ||= options[:web_root] + File.join(attr)
    FileUtils.mkpath([ options[:store_dir], options[:tmp_base_dir] ])
    options
  end
...

  def file_column(attr, options={})
    options = DEFAULT_OPTIONS.merge(options) if options
    my_options = FileColumn::init_options(options,
                                          attr.to_s)
  end
...
end

Thanks in advance
This topic is locked and can not be replied to.