file_field (like text_field, check_box) and so on assume that its first
parameter is the name of a instance variable and the second the name of
a method that can be called on that instance variable.
So here file_field will try to do @picture.picture, which i’m assuming
fails because @picture is nil.
You probably want something like file_field(“house”, picture) (or
possibly you just want to use file_field_tag)
file_field (like text_field, check_box) and so on assume that its first
parameter is the name of a instance variable and the second the name of
a method that can be called on that instance variable.
So here file_field will try to do @picture.picture, which i’m assuming
fails because @picture is nil.
You probably want something like file_field(“house”, picture) (or
possibly you just want to use file_field_tag)
Fred
Ok, thanks. I’ll try that this evening. Just for more information, I
have a PICTURES table and a HOUSES table. The HOUSES table has a
foreign key of “picture_id” to the pictures table. When I’m saving the
fields for a new house, I’m using the file_field to browse for an image.
Then, in the “create” method in my house_controller I’m using:
@house = House.new(params[:house]) @picture = Picture.new(params[:picture])
if @picture.save @house.picture_id = @picture.id
end
#… #continue with @house processing…
Everything works fine, except when I try to call “/house/edit/1” when
the house entry for id of 1 does not have a picture with it. So I
thought by using “@house.picture_id” I could check that field to see if
it was empty, and if it was, display the “file_field” again. Does your
reply still apply now that you know how I’m doing things? Is there a
way to check for a null “picture_id” field, and show a file_field to
upload an image if so? Thanks!
First of all, I was reading a little hastily this morning, and actually
file_field is a little different to the other blah_field helpers - the
others use the parameters to get the current value of the attribute as
their default, which file_field doesn’t, so file_field(“picture”,
“picture”) shouldn’t really care about the actual value of @picture
Yes, you can use house.picture_id or house.picture to tell if a house
does or does not have a picture; however given that you then go on to
use @picture, the most intuitive thing for me would be <% if @picture %>
What would really be helpful too, is the exact error that you get on the
page.
First of all, I was reading a little hastily this morning, and actually
file_field is a little different to the other blah_field helpers - the
others use the parameters to get the current value of the attribute as
their default, which file_field doesn’t, so file_field(“picture”,
“picture”) shouldn’t really care about the actual value of @picture
Yes, you can use house.picture_id or house.picture to tell if a house
does or does not have a picture; however given that you then go on to
use @picture, the most intuitive thing for me would be <% if @picture %>
What would really be helpful too, is the exact error that you get on the
page.
Fred
Ok, thanks. I’ll post the exact error when I get home from work this
evening.
It was ignorance on my part for not checking for the @house.picture_id
in the controller BEFORE attempting to setup the @picture object. So
this is what I did:
if @house.picture_id != nil @picture = Picture.find(@house.picture_id)
end
That worked like a charm. Thanks for all the help. However, if you
don’t mind, can you help me with another problem? I was debating saving
to the database vs. a filesystem, and was trying to get the filesystem
to work by doing this:
@house = House.new(params[:house]) @filename = @params[:picture][:picture].original_filename
File.open("#{RAILS_ROOT}/public/images/#{@filename}", “wb”) do |f|
f.write(@params[:picture][:picture].read)
end
That works, but it writes a “0kb” file to the directory. How can I get
it to actually put the file data there??? Thanks for any advice.
It was ignorance on my part for not checking for the @house.picture_id
in the controller BEFORE attempting to setup the @picture object. So
this is what I did:
if @house.picture_id != nil @picture = Picture.find(@house.picture_id)
end
That worked like a charm. Thanks for all the help. However, if you
don’t mind, can you help me with another problem? I was debating saving
to the database vs. a filesystem, and was trying to get the filesystem
to work by doing this:
@house = House.new(params[:house]) @filename = @params[:picture][:picture].original_filename
File.open(“#{RAILS_ROOT}/public/images/#{@filename}”, “wb”) do |f|
f.write(@params[:picture][:picture].read)
end
That works, but it writes a “0kb” file to the directory. How can I get
it to actually put the file data there??? Thanks for any advice.
I highly recommend the FlexImage plugin for this, and not just because I
wrote it.
With FlexImage in charge of your Picture model, you can have it read and
write the binary data either to the database or to disk or the very
easily. Uploads and binary data storage are all handled for you without
any complicated code.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.