I have the following form in my view (one file field, and one
textarea). Both fields are optional---if the text field is filled, a
row will be created, and if a file is uploaded, a row will be created.
It is possible to submit both.
<%= form_tag({:action => ''create''}, :multipart =>
true) %>
<p><%= text_area_tag(''text'', '''',
:cols => 85, :rows => 7) %></p>
<p><%= file_field_tag(''file'') %></p>
<%= submit_tag ''Add Item'' %>
<%= end_form_tag %>
In the ''create'' action I have this code:
if @request[''text''].length > 0
@new_item = ClipboardItem.new
@new_item.postdate = DateTime.now
@new_item.payload_type = ''text''
if @request[''text''].kind_of? StringIO
@new_item.payload = @request[''text''].string
else
@new_item.payload = @request[''text''].read
end
@new_item.save
end
if @request[''file''].length > 0
@new_item = ClipboardItem.new
@new_item.postdate = DateTime.now
@new_item.payload_type = @request[''file''].content_type
@new_item.payload = @request[''file''].read
@new_item.uploaded_file = @request[''file'']
@new_item.save
end
When I upload text or file exclusively, it works fine. However, when I
upload both, the text gets lost for some reason. I get an empty string
in the database (so the row gets created). What is happening to the
text file data? (The file always seems to go through fine.)
Sincerely,
Tom Reinhart
tom-V0YqjHVuocLQT0dZR+AlfA@public.gmane.org
http://AllTom.com/
Check if your HTML output contains method="post" on the form tag. Some browsers get confused if they don''t get it. --- Thomas Am 01.05.2005 um 7:38 schrieb Tom Reinhart:> I have the following form in my view (one file field, and one > textarea). Both fields are optional---if the text field is filled, a > row will be created, and if a file is uploaded, a row will be created. > It is possible to submit both. > > <%= form_tag({:action => ''create''}, :multipart => true) %> > <p><%= text_area_tag(''text'', '''', :cols => 85, :rows => 7) %></p> > <p><%= file_field_tag(''file'') %></p> > <%= submit_tag ''Add Item'' %> > <%= end_form_tag %> > > In the ''create'' action I have this code: > > if @request[''text''].length > 0 > @new_item = ClipboardItem.new > @new_item.postdate = DateTime.now > @new_item.payload_type = ''text'' > if @request[''text''].kind_of? StringIO > @new_item.payload = @request[''text''].string > else > @new_item.payload = @request[''text''].read > end > @new_item.save > end > if @request[''file''].length > 0 > @new_item = ClipboardItem.new > @new_item.postdate = DateTime.now > @new_item.payload_type = @request[''file''].content_type > @new_item.payload = @request[''file''].read > @new_item.uploaded_file = @request[''file''] > @new_item.save > end > > When I upload text or file exclusively, it works fine. However, when I > upload both, the text gets lost for some reason. I get an empty string > in the database (so the row gets created). What is happening to the > text file data? (The file always seems to go through fine.) > > Sincerely, > > Tom Reinhart > tom-V0YqjHVuocLQT0dZR+AlfA@public.gmane.org > http://AllTom.com/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
-I am not formally educated on database design-
I have a similar data structure, some primary object can have a
plaintext asset and/or a binary asset.
I have it so the primary object has_one of each of the different
assets (which have their own tiny models, just the required id /
foreign key and the columns to describe the asset, a longtext or
longblob respectively [more or less]).
Then i have a method for my primary object updated_assets… which just
create_plain_asset and/or create_binary_asset… and in the binary asset
model I have the code to put the file in the DB in a before_save
maybe something like this could work for you
*none of this code is tested*
class ClipboardItem …
…
has_one :binary_asset, :class_name => "Bin", :dependent => true
has_one :plain_asset, :class_name => "Plain", :dependent =>
true
def update_assets(params)
if create_plain_asset(params[:plain])
return create_binary_asset(params[:bin])
end
false
end
end
and then just copy and paste the stuff from the scaffold generated
_form.rhtml for the plain and bin model into your other form.
your create could become something close to this
def create
@clipboarditem = ClipboardItem.new(@params[:clipboarditem])
if @clipboarditem.save && @clipboarditem.update_assets(@params)
flash['notice'] = 'clipboarditem was successfully
created.'
redirect_to :action => 'list'
else
render_action 'new'
end
end
On 5/1/05, Thomas Fuchs <thomas@fesch.at> wrote:> Check if your HTML output contains method="post" on the form tag.
> Some browsers get confused if they don't get it.
>
> ---
> Thomas
>
> Am 01.05.2005 um 7:38 schrieb Tom Reinhart:
>
> > I have the following form in my view (one file field, and one
> > textarea). Both fields are optional---if the text field is filled, a
> > row will be created, and if a file is uploaded, a row will be created.
> > It is possible to submit both.
> >
> > <%= form_tag({:action => 'create'}, :multipart =>
true) %>
> > <p><%= text_area_tag('text', '', :cols =>
85, :rows => 7) %></p>
> > <p><%= file_field_tag('file') %></p>
> > <%= submit_tag 'Add Item' %>
> > <%= end_form_tag %>
> >
> > In the 'create' action I have this code:
> >
> > if @request['text'].length > 0
> > @new_item = ClipboardItem.new
> > @new_item.postdate = DateTime.now
> > @new_item.payload_type = 'text'
> > if @request['text'].kind_of? StringIO
> > @new_item.payload = @request['text'].string
> > else
> > @new_item.payload = @request['text'].read
> > end
> > @new_item.save
> > end
> > if @request['file'].length > 0
> > @new_item = ClipboardItem.new
> > @new_item.postdate = DateTime.now
> > @new_item.payload_type = @request['file'].content_type
> > @new_item.payload = @request['file'].read
> > @new_item.uploaded_file = @request['file']
> > @new_item.save
> > end
> >
> > When I upload text or file exclusively, it works fine. However, when I
> > upload both, the text gets lost for some reason. I get an empty string
> > in the database (so the row gets created). What is happening to the
> > text file data? (The file always seems to go through fine.)
> >
> > Sincerely,
> >
> > Tom Reinhart
> > tom@alltom.com
> > http://AllTom.com/
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
On 5/1/05, Caleb Buxton <adbust@gmail.com> wrote:> I have a similar data structure, some primary object can have a > plaintext asset and/or a binary asset. > > I have it so the primary object has_one of each of the different > assets (which have their own tiny models, just the required id / > foreign key and the columns to describe the asset, a longtext or > longblob respectively [more or less]). > > Then i have a method for my primary object updated_assets… which just > create_plain_asset and/or create_binary_asset… and in the binary asset > model I have the code to put the file in the DB in a before_save > > maybe something like this could work for youI really like the way that you wrapped it up, and I think that I will probably do the same to separate my text and binary assets. So thanks! However, my main issue is accessing the submitted text when there is a file attachment. It is as if the text data is 'rubbed away' if the user both uploads a file and typed text in the form, and I can't tell why. At this point, I think I will wait for newer versions of Rails and use two different forms for now, since I'm not sure how to debug something like this.> > Am 01.05.2005 um 7:38 schrieb Tom Reinhart: > > > > > I have the following form in my view (one file field, and one > > > textarea). Both fields are optional---if the text field is filled, a > > > row will be created, and if a file is uploaded, a row will be created. > > > It is possible to submit both. > > > > > > <%= form_tag({:action => 'create'}, :multipart => true) %> > > > <p><%= text_area_tag('text', '', :cols => 85, :rows => 7) %></p> > > > <p><%= file_field_tag('file') %></p> > > > <%= submit_tag 'Add Item' %> > > > <%= end_form_tag %> > > > > > > In the 'create' action I have this code: > > > > > > if @request['text'].length > 0 > > > @new_item = ClipboardItem.new > > > @new_item.postdate = DateTime.now > > > @new_item.payload_type = 'text' > > > if @request['text'].kind_of? StringIO > > > @new_item.payload = @request['text'].string > > > else > > > @new_item.payload = @request['text'].read > > > end > > > @new_item.save > > > end > > > if @request['file'].length > 0 > > > @new_item = ClipboardItem.new > > > @new_item.postdate = DateTime.now > > > @new_item.payload_type = @request['file'].content_type > > > @new_item.payload = @request['file'].read > > > @new_item.uploaded_file = @request['file'] > > > @new_item.save > > > end > > > > > > When I upload text or file exclusively, it works fine. However, when I > > > upload both, the text gets lost for some reason. I get an empty string > > > in the database (so the row gets created). What is happening to the > > > text file data? (The file always seems to go through fine.) > > > > > > Sincerely, > > > > > > Tom Reinhart > > > tom@alltom.com > > > http://AllTom.com/ > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/railsSincerely, Tom Reinhart tom@alltom.com http://AllTom.com/ _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails