Safari and Firefox give different answers

I’am creating an image uploading system for advertise site in Rails
Using receipt 57 from the book rails recipies.

1 action give different returns in safari and Firefox

def file_data=(file_data)
if file_data.blank?
else
@file_data = file_data
write_attribute ‘extension’ ,
file_data.original_filename.split( ‘.’ ).last.downcase
end
end

when a create an advert in Safari without an image the file_date.blank?
works well and i can save my advert without an picture.
But firefox ignores the file_data.blank? so i get an no method error

#You have a nil object when you didn’t expect it!
#The error occured while evaluating nil.downcase

#{RAILS_ROOT}/app/models/advert.rb:51:in file_data=' #{RAILS_ROOT}/app/controllers/home_controller.rb:111:increateadvert’

Firefox ignores the if file_data.blank?
How can i made that both browsers do the same thing and works Ie the
same like fireox in this case

Ruby code its not processed by the browsers… the only matter is that
the Firefox its sending something and Safari not…

To see it deep I need the code of the view and the controller…

And, please, don’t use an if with no code inside… use better:

def file_data=(file_data)
unless file_data.blank?
@file_data = file_data
write_attribute ‘extension’ ,
file_data.original_filename.split( ‘.’ ).last.downcase
end
end

1 action give different returns in safari and Firefox

def file_data=(file_data)
if file_data.blank?
else
@file_data = file_data
write_attribute ‘extension’ ,
file_data.original_filename.split( ‘.’ ).last.downcase
end
end

when a create an advert in Safari without an image the file_date.blank?
works well and i can save my advert without an picture.
But firefox ignores the file_data.blank? so i get an no method error

#You have a nil object when you didn’t expect it!
#The error occured while evaluating nil.downcase

#{RAILS_ROOT}/app/models/advert.rb:51:in file_data=' #{RAILS_ROOT}/app/controllers/home_controller.rb:111:increateadvert’

Firefox ignores the if file_data.blank?
How can i made that both browsers do the same thing and works Ie the
same like fireox in this case

In my controller i have this

def newadvert
@advert = Advert.new

end

def createadvert
user = @session[‘user’]
@advert = Advert.new(params[:advert])
if @advert.save
redirect_to :action => ‘saveadvert’, :id => @advert
else
render :action => ‘newadvert’
end
end

      def saveadvert
        user = @session['user']
         @advert = Advert.find(params[:id])
        if @advert.update_attributes(params[:advert])
          @advert.update_attribute "user_id", user.id
             @advert.update_attribute "username", user.login
             @advert.update_attribute "town", user.town
             @advert.update_attribute "province", user.province
             @advert.update_attribute "email", user.email
             @advert.update_attribute "phone", user.phone
             @advert.update_attribute "mobile_phone", 

user.mobile_phone
@advert.save
flash[:notice] = “Bedankt voor het plaatsen van uw
advertentie”
redirect_to :controller => “home”, :action =>
“showadvert”, :id => @advert
end
end

This is te view of mine site

<%= error_messages_for ‘advert’ %>

Adverteer

Velden met een * zijn verplichte velden.

<% form_for :advert, @advert, :url=> {:action => ‘createadvert’ },
:html=>{:multipart=>true} do |f| %>

<%= render :partial => ‘form’ %>

Image File:
<%= f.file_field :file_data %>

<%= options = Advert::OBJECT_TYPE
select(“advert”, “object_type”, options)
%>
<%= submit_tag “Adverteer”, :confirm => ‘Are you sure?’ %>
<%= link_to ‘Back’, :action => ‘list’ %>

<% end %>

I find out that firexox send a string like
“file_data”=>#StringIO:0x24d4b28

And safari sends
“file_data”=>""

How can i handle the different pieces of code the controller recieves

def createadvert
user = @session[‘user’]
@advert = Advert.new(params[:advert])
if @advert.save
redirect_to :action => ‘saveadvert’, :id => @advert
else
render :action => ‘newadvert’
end
end

I just tried the code and works fine in Firefox… I don’t know what’s
the problem…mmm. Try restarting the server process…

Another thing I have seen in your code… Why you save and advert and
after redirect to saveadvert and update some attributes and save it
again?

And one thing more, … don’t use @session, use session. It’s better
using the method not the variable.

Moises D. wrote:

def createadvert
user = @session[‘user’]
@advert = Advert.new(params[:advert])
if @advert.save
redirect_to :action => ‘saveadvert’, :id => @advert
else
render :action => ‘newadvert’
end
end

I just tried the code and works fine in Firefox… I don’t know what’s
the problem…mmm. Try restarting the server process…

I found out Firefox send an StringIO while Safari sends an blank
file_data
unless file_data == “StringIO:0x24d4b28” doesn’nt work beause the
string is different each time

Another thing I have seen in your code… Why you save and advert and
after redirect to saveadvert and update some attributes and save it
again?

That’s because when i do the update attributes into the createadvert the
validates_prescence will say the advert is’nt complete but the
incomplete advert wil be sved with the other atributes

I found out Firefox send an StringIO while Safari sends an blank
file_data
unless file_data == “StringIO:0x24d4b28” doesn’nt work beause the
string is different each time

Its a strange error…

If you try to upload a real file, what kind of object do you receive?

That’s because when i do the update attributes into the createadvert the
validates_prescence will say the advert is’nt complete but the
incomplete advert wil be sved with the other atributes

Thinking about your code…, I just think that the user information
should be linked to the advert using an association (has_one)… maybe,
if the User its a ActiveRecord model.
If the user is associated with the advert you only need to store the
user_id on the advert and you can do all in the createadvert action.

Firefox sends code like this wheb the file field is empty

“file_data”=>#StringIO:0x24735a8 how can i tell the model that it must
ignore this information

I tried to save an user_id withot the action saveadvert it doesn’t work
at this moment.

But my biggest problem at this moment is that firefox send information
to the model that brings an error.

GA Gorter wrote:

Firefox sends code like this wheb the file field is empty

“file_data”=>#StringIO:0x24735a8 how can i tell the model that it must
ignore this information

And when you try to upload a file…, what value have file_data??
It’s to see the differences…

After taking some rest for a day i tried again, and found a solotion for
firefox.

def file_data=(file_data)
@firefox = file_data
return if file_data.blank?
return if file_data == @firefox
@file_data = file_data
write_attribute ‘extension’ ,
file_data.original_filename.split( ‘.’ ).last.downcase
end

So for safari the computer uses

return if file_data.blank?

And for Firefox it works

@firefox = file_data
return if file_data == @firefox

I believe this solotion is not so reprensative but i don’t know how i
can make 1 function for both browsers

I’am hoping internet explorer won’t have problems with this wile i can’t
test on internet explorer