Why file_field helper returns as a String object instead of a File object in fields_for?

I use scaffold generated an new.html.erb file and change it like this:

New article

<%= error_messages_for :article %> <% form_for(@article) do |f| %>

Title
<%= f.text_field :title %>

Body
<%= f.text_area :body %>

news
<%= f.select( :newscata_id, Newscata.find(:all).collect {|p| [ p.name, p.id ] }) %>

<% for newsfile in @article.newsfiles %> <% fields_for "article[newsfile_attributes][]", newsfile do | newsfile_form|%>

attach:<%= newsfile_form.text_field :name %> file:<%= newsfile_form.file_field "uploaded_picture" %>

<% end %> <% end %>

Create at
<%= f.datetime_select :create_at %>

<%= f.submit "Create" %>

<% end %> <%= link_to 'Back', articles_path %> when this page post back , the request parametes are :

{“article”=>{“newsfile_attributes”=>[{“name”=>“sdafasdfas”,
“uploaded_picture”=>“ʹÓÃTextMate½øÐÐRails¿ª·¢ - rails - Ruby - JavaEyeÂÛ
̳.htm”},
{“name”=>"",
“uploaded_picture”=>""}],
“title”=>“asdfa”,
“body”=>“sfdsafasdf”,
“create_at(1i)”=>“2008”,
“create_at(2i)”=>“5”

what’s the problem? can anyone help me? thanks!

<% form_for(@article, :html => {:multipart => true}) do |f| %>

without making that form handle multipart, the file field will return a
string even in regular HTML. it’s a common gotcha. hope that helps.

RSL

thanks a lot !
that works !

Mr. Hueywong,
I have a similar problem in that I’m trying to deliver a .csv file to
my application so that it can be processed (imported) and then
discarded. I posted this about 4 times now and don’t seem to hit
paydirt so here goes again:
I’ve written a routine allowing the client user to declare where
their .csv import file resides and then send it to the server to
import the many rows into an existing model table. I’ve imported many
graphic files into my application using attachment_fu and this was
very straightforward as they were imported into an existing model
object. Instead, I am trying to import a file, that when it’s
processed will be discarded. These examples are running fine, but I
won’t be able to use this strategy in my production environment…
Here’s my view code:

<%= form_tag(:url => {:action=> “post” }, :html => { :multipart =>
true }) -%>

<%= file_field_tag "file", :size => 75, :class => "csv-input" -%>

File to Upload

<%= submit_tag -%>

Here’s my controller code:

class ImportController < ApplicationController
def aaccount_import
n = 0
unless params[:file].nil?
FasterCSV.foreach("#{RAILS_ROOT}/convert/#{params[:file]}") do |
row| #here I have to ‘hard code’ the file location for my
development environment
c = Account.new
c.code = row[0]
c.description = row[1]
if c.save
n += 1
else
n += 1
d = Badboy.new
d.message = ‘Aaccount Record Failed to convert’
d.recno = n
d.save
end
end
end
end
end

I am not importing the file in this routine but only processing it.
The ‘file’ param is really a string and it needs to be a file
‘hanging’ out on my server until it is processed. I am grateful for
any links or suggestions.
Kathleen

On May 10, 2:36 am, “[email protected][email protected]
wrote:

<%= form_tag(:url => {:action=> “post” }, :html => { :multipart =>
true }) -%>

Hi Kathleen,
Your problem is in the line above - the syntax for form_tag is
different from form_for, so the :multipart => true attribute isn’t
being passed into the form tag correctly. You want:
<%= form_tag({:action=> “post” }, {:multipart => true}) -%>

  • Matt

It looks like your action is called “aaccount_import”, not “post”.
The method will be “post” by default for a form (of this type,
anyway). Try:

<%= form_tag({:action=> “aaccount_import” }, {:multipart => true}) -
%>

-Kyle

On May 10, 2:36 pm, “[email protected][email protected]

Matt,
When I try that I get the "Unknown action No action responded to post
". This works fine the way it is, but I only get a string passed. I am
lost when trying to unravel this?
Kathleen

You guy’s are great! It turns out that the reason I thought

<%= form_tag(:url => {:action=> “post” }, :html => { :multipart =>
true }) -%>

was the way to go is because my FasterCSV line:

FasterCSV.foreach("#{RAILS_ROOT}/convert/#{params[:file]}") do |

would COUGH if it got a true file object. Thus, the above was sending
a string which made FasterCSV happy.
Now that I am sending FasterCSV a file object, the line looks like
this;

FasterCSV.new(params[:file]).each do |row|

I am happy to share my CSV import code should anyone want to see it.
Thanks again,
Kathleen

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs