huyewong
2008-May-09 10:38 UTC
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:
<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!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Russell Norris
2008-May-09 11:29 UTC
Re: why file_field helper returns as a String object instead of a File object in fields_for?
<% 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 Fri, May 9, 2008 at 6:38 AM, huyewong <huyewong@yahoo.com.cn> wrote:
>
> 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!
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
huyewong
2008-May-09 12:28 UTC
Re: why file_field helper returns as a String object instead of a File object in fields_for?
thanks a lot ! that works ! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-May-10 01:36 UTC
Re: why file_field helper returns as a String object instead of a File object in fields_for?
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 May 9, 4:38 am, huyewong
<huyew...-/E1597aS9LRv1O+Z8WTAqQ@public.gmane.org>
wrote:> 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!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Matt Westcott
2008-May-10 18:33 UTC
Re: why file_field helper returns as a String object instead of a File object in fields_for?
On May 10, 2:36 am, "KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-May-10 19:36 UTC
Re: why file_field helper returns as a String object instead of a File object in fields_for?
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 May 10, 12:33 pm, Matt Westcott <matt...-EVGsuJHbCVyNPqACXyvcFA@public.gmane.org> wrote:> On May 10, 2:36 am, "KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Kyle
2008-May-10 23:51 UTC
Re: why file_field helper returns as a String object instead of a File object in fields_for?
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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 May 10, 12:33 pm, Matt Westcott
<matt...-EVGsuJHbCVyNPqACXyvcFA@public.gmane.org> wrote:
>
> > On May 10, 2:36 am,
"KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-May-11 14:26 UTC
Re: why file_field helper returns as a String object instead of a File object in fields_for?
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
On May 10, 5:51 pm, Kyle
<kyle.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> wrote:
>
>
>
> > 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 May 10, 12:33 pm, Matt Westcott
<matt...-EVGsuJHbCVyNPqACXyvcFA@public.gmane.org> wrote:
>
> > > On May 10, 2:36 am,
"KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<KathysK...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > > 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- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---