I use scaffold generated an new.html.erb file and change it like this:
<h1>New article</h1>
<%= error_messages_for :article %>
<% form_for(@article) do |f| %>
<p>
<b>Title</b><br />
<%= f.text_field :title %>
</p>
<p>
<b>Body</b><br />
<%= f.text_area :body %>
</p>
<p>
<b>news</b><br />
<%= f.select( :newscata_id, Newscata.find(:all).collect {|p|
[ p.name, p.id ] }) %>
</p>
<p>
<% for newsfile in @article.newsfiles %>
<% fields_for "article[newsfile_attributes][]", newsfile do |
newsfile_form|%>
<p>
attach:<%= newsfile_form.text_field :name %>
file:<%= newsfile_form.file_field "uploaded_picture" %>
</p>
<% end %>
<% end %>
</p>
<p>
<b>Create at</b><br />
<%= f.datetime_select :create_at %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% 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!
on 09.05.2008 12:47
on 09.05.2008 13:30
<% 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
on 09.05.2008 14:29
thanks a lot ! that works !
on 10.05.2008 03:37
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 }) -%>
<p><%= file_field_tag "file", :size => 75, :class => "csv-input" -%></
p>
<p><strong><label for="file">File to Upload</label></strong></p>
<p><%= submit_tag -%></p>
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 10.05.2008 20:34
On May 10, 2:36 am, "KathysK...@gmail.com" <KathysK...@gmail.com> 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
on 10.05.2008 21:37
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
on 11.05.2008 01:52
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, "KathysK...@gmail.com" <KathysK...@gmail.com>
on 11.05.2008 16:27
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