Help with FlexImage


#1

I am trying to use FlexImage after droping file_column but can not
seem to get it to work, please help.

Model

class LibResource < FlexImage::Model
self.require_image_data = false
pre_process_image :size => ‘250x250’

end

Controller

class CreationController < ApplicationController
flex_image :action => ‘show’,:class => LibResource # I don’t get this
line and docs are lacking

def create_module
@mod = LibResource.new
if request.post?
@mod.attributes = params[:mod]

end
end

Form View

<%= start_form_tag( {:action => ‘create_module’, :id => @mod, :type =>
@mod.type}, {:multipart => true} )%>

Image:
<%= file_field ‘mod’, ‘image’ %>


<%= end_form_tag %>

Display View

<%= image_tag( url_for(:controller => ‘creation’, :action =>
‘show’, :id => @mod)) %> #heres that show method again?

Model


image MEDIUMBLOB

I want to be able to upload an image file to the LibResource Model and
store it in the DB and then finish creating other things

I then want to be able to show a page with the LibResource Model
data(including the image) and other Model’s data

Right now, instead of the image being displayed, just the string of
the id is displayed

HTML Output

  • 53
  • And no there is not a file called show in the creation folder(which is
    a view folder)

    This just seems too complicated. I got file_column working fairly
    easily but then had other issues with it. I wish the FlexImage Docs
    were better.

    Thanks in advance - K


    #2

    Now:

    <%= image_tag image_url(:id => @mod) %>

    Should return:

    I knew I would screw something up in that book I just wrote.

    that image_tag should be:

    <%= image_tag image_url(
    :controller => ‘lib_resources’,
    :action => ‘image’, #the action name that renders images
    :id => 123
    ) %>


    #3

    Controller

    class CreationController < ApplicationController
    flex_image :action => ‘show’,:class => LibResource # I don’t get this
    line and docs are lacking

    That is a macro which makes your controller render image data instead of
    HTML. When the action “show” is called, and there is an :id in your
    params, it will render the requested image. The :action portion of that
    should name an action that is dedicated to rendering image data. If
    your controller already has a show action in it for showing
    LibResource’s then you need to declare a different method, such as
    “show_image” or perhaps just “image”.

    Although the recommended current usage is to use flexi views:

    #controller
    def show_image
    @image = LibResource.find(params[:id])
    end

    #app/views/some_controller_name/show_image.flexi
    @image.resize! :size => “123x456”

    Just note you need rails 1.2 or newer for flexi views. I really need to
    spend some time updating the docs.

    Display View

    <%= image_tag( url_for(:controller => ‘creation’, :action =>
    ‘show’, :id => @mod)) %> #heres that show method again?

    As you found out, image_tag will append .png to the url automatically if
    the url has no extension. This doesn’t work. I recommend to define a
    custom route with a .jpg extension.

    #Rails >= 1.2
    map.image ‘:controller/:action/:id.jpg’

    #Rails < 1.2
    map.image ‘:controller/:action/:id/image.jpg’

    Now:

    <%= image_tag image_url(:id => @mod) %>

    Should return:

    You should be able to go to your action, with an id, and see an image.
    SO point your browser to /lib_resources/image/123.jpg and you should get
    an image in your browser.

    Model


    image MEDIUMBLOB

    by default the binary column should be named “data”. If you want it to
    be named differently, use this snippet in your class definition:

    class LibResource < FlexImage::Model
    binary_column :image
    end

    I want to be able to upload an image file to the LibResource Model and
    store it in the DB and then finish creating other things

    Shouldn’t be a problem, although ActiveRecord won’t let you save a
    record unless it passes validation. So as long as your model can
    validate with only the image saved in it this wont be a problem.
    Otherwise you may have to change your approach to upload the file either
    during or after the rest of the model’s data is populated.

    I then want to be able to show a page with the LibResource Model
    data(including the image) and other Model’s data

    As long as the image action is different from the show action, this
    should be as easy as the image_tag I posted above.

    Right now, instead of the image being displayed, just the string of
    the id is displayed

    This is most likely firefox showing the alt tag of an image that it
    couldn’t display. If you point the src of the image to the url that is
    dedicated to rendering images it should render properly.

    This just seems too complicated. I got file_column working fairly
    easily but then had other issues with it. I wish the FlexImage Docs
    were better.

    Sorry, but I am a better programmer than I am documentation writer. I
    also seem to be having a problem keeping that docs up to date. There is
    no excuse on my part for the latter. If any FlexImage fans want to help
    make the docs rock my socks I’m all for it. Until then I will limp
    along to explain things as best I can.

    Good luck
    Alex

    PS: I get RSS notification of every comment on my blog you need further
    help. I don’t always see the messages here.


    #4

    Thanks Alex,
    I am getting closer, but still not there.

    I renamed the MediumBlob column to data and made a show_image function
    and show_image.flexi view. I made the other changes you suggested too.
    I am still only seeing the alt tag but at least the HTML output looks
    better. It is still adding a .png to the end (I added the custom
    route, but it seems to not be working.)

    Controller

    class CreationController < ApplicationController
    flex_image :action => ‘show_my_image’,:class => LibResource

    def show_my_image
    @image = LibResouce.find(params[:id]).image
    end

    Views

    #show_image.flexi
    @image.resize! :size => “250x250” #do I need to call the size/resize
    action if I am pre-processing the image in the model?

    #display view
    <%= image_tag image_url(:controller => ‘lib_resources’, :action =>
    ‘show_image’, :id => @mod) %>

    Routes.rb

    map.image ‘:controller/:action/:id.jpg’ # is this right?

    HTML Output

    56

    Thanks in advance --K

    On Feb 8, 11:23 pm, Alex W. removed_email_address@domain.invalid


    #5

    of course - brain fart/typo - fixed it

    Views

    #show_image.flexi
    @image.resize! :size => “250x250” #do I need to call the size/resize
    action if I am pre-processing the image in the model?

    If that’s really the case all you need in your view file is:

    @image

    I meant that I was doing this in the model:
    pre_process_image :size => ‘250x250’ # isn’t this a FlexImage call?

    map.image ‘:controller/:action/:id/image.jpg’

    No I have not updated to 1.2 yet - but I tried both ways and neither
    worked
    I removed the flexi file and put this line back in my controller:
    flex_image :action => ‘show_image’,:class => LibResource

    I am still only seeing an alt tag

    HTML

    <img alt=“57” src=“http://localhost:3002/creation/show_image/57.png” /

    And this error in my log files:
    ActionController::MissingTemplate (Missing template C:/Documents and
    Settings/griggsk/user/ICA/config/…/app/views/creation/
    show_image.rhtml):

    Here are some facts/questions:
    I have a model LibResource that inherits from FlexImage
    I have a controller Creation that has the ‘show_image’ action
    So is this line correct?
    <%= image_tag image_url(:controller => ‘creation’, :action =>
    ‘show_image’, :id => @mod.id) %>

    Am I just totally off? Should I update to 1.2?

    Thanks for your continued help - K


    #6

    Kim wrote:

    flex_image :action => ‘show_my_image’,:class => LibResource

    Get rid of this line if you are using a flexi view.

    def show_my_image
    @image = LibResouce.find(params[:id]).image
    end

    The name of this action here needs to match

    <%= image_tag image_url(:controller => ‘lib_resources’, :action =>
    ‘show_image’, :id => @mod) %>

    the name of the action here. So you have an action on your controller
    called “show_my_image” that renders an image, and in your view you are
    calling the action “show_image”. Obviously this will not work.

    Views

    #show_image.flexi
    @image.resize! :size => “250x250” #do I need to call the size/resize
    action if I am pre-processing the image in the model?

    If that’s really the case all you need in your view file is:

    @image

    Your view file is supposed to transform your image for output. If you
    want no transformations, just return the @image object. If you want to
    process the image in any way before viewing, which is really what
    FlexImage was designed for, then you would od that in the flexi view.

    Routes.rb

    map.image ‘:controller/:action/:id.jpg’ # is this right?

    HTML Output

    56

    Are you sure you are on rails 1.2? Try this route instead:

    map.image ‘:controller/:action/:id/image.jpg’


    #7

    Kim wrote:

    I meant that I was doing this in the model:
    pre_process_image :size => ‘250x250’ # isn’t this a FlexImage call?

    This processes the image at upload. This means the best possible image
    you can get out of this uploaded image is 250x250 resolution. FlexImage
    is good at having a high res master image, and then displaying both a
    thumbnail and a full size image based on that. So if you never want to
    show the image at any resolution higher than 250x250 then this is fine.

    Also, in order to use flexi views you need rails 1.2, as I stated above.

    on rails 1.1.6 use:

    flex_image :action => ‘show_image’, :class => LibResource

    or if you prefer, this will give a bit more control over processing of
    the image:

    def show_image
    @image = LibResource.find(params[:id])
    render_flex_image(@image)
    end

    Both of these options will work pre-rails 1.2. .flexi views will not.

    map.image ‘:controller/:action/:id/image.jpg’

    I am still only seeing an alt tag

    HTML

    <img alt=“57” src=“http://localhost:3002/creation/show_image/57.png” /

    Leave the HTML out of it for now. Go to
    http://localhost:3002/creation/show_image/57 and you should either an
    image, or an error telling you what went wrong.

    I’m not exactly sure why that route isn’t working. Try making sure the
    route is above the default ‘:controller/:action/:id’ route.

    Here are some facts/questions:
    I have a model LibResource that inherits from FlexImage
    I have a controller Creation that has the ‘show_image’ action
    So is this line correct?
    <%= image_tag image_url(:controller => ‘creation’, :action =>
    ‘show_image’, :id => @mod.id) %>

    Should work, yes. If you still can’t get your route to work, try:

    Am I just totally off? Should I update to 1.2?

    No and Yes.

    Thanks for your continued help - K

    No problem! If you can think of a way to make this easier to understand
    or implement I am all ears.


    #8

    Still Stuck on this.


    #9

    THANKS! That worked. The problem was this:
    Try making sure the route is above the default
    ‘:controller/:action/:id’
    route.

    I have a couple more questions/problems if your up to it.

    1. When editing the model that contains the image, the field for the
      image
      is blank. I think users will get confused and think they have to upload
      the
      image again. Is there a way to put a value in that field?

    2.I need to be able to make a copy of the model with the image. Copying
    the
    model makes a new row in the database with a new id. The idea is to
    allow
    users to add the model with the image on more than one webpage - but
    also
    let them edit the model on one page without effecting the model on the
    original page - that is why I need multiple copies in the database -
    each
    page points to it’s own image model.
    The problem is that I can not copy the model with the image. I am not
    getting any errors - its just not saving the model. How can I make a
    copy
    of the model with an image?

    Code (this works fine for the models that no not have an image)

    class CreationController < ApplicationController

    def add_copy
    @mod = @user.find_mod(params[:id],params[:type] ) #returns the model
    @mod_copy = (@mod.type).new #creates a new copy of the model
    @mod_copy.attributes = @mod.attributes #gets the models attributes

    this must be the problem because this is where the copy should get the
    image
    if @mod_copy.save #quietly fails on the model with the image

    If you have any suggestions on this issues I would greatly appreaciate
    it.

    Thanks for all your help - K

    On 2/9/07, Alex W. removed_email_address@domain.invalid wrote:

    show the image at any resolution higher than 250x250 then this is fine.
    def show_image

    route is above the default ‘:controller/:action/:id’ route.

    or implement I am all ears.


    Posted via http://www.ruby-forum.com/.


    Kim G.
    removed_email_address@domain.invalid

    “We are all stakeholders in the Karma economy.”


    #10

    Kim G. wrote:

    THANKS! That worked. The problem was this:
    Try making sure the route is above the default
    ‘:controller/:action/:id’
    route.

    Sweet!

    I have a couple more questions/problems if your up to it.

    1. When editing the model that contains the image, the field for the
      image
      is blank. I think users will get confused and think they have to upload
      the
      image again. Is there a way to put a value in that field?

    Not really. The file upload field is supposed to be the local path to
    the file to be uploaded. If you put some strange value in there, the
    browser will try to upload whatever local path is in there, and your
    app will process it.

    Other apps like shopify.com solve this by showing the image, if one
    exists. Then under it say “upload a new image” and have the upload
    field.

    2.I need to be able to make a copy of the model with the image. Copying
    the
    model makes a new row in the database with a new id. The idea is to
    allow
    users to add the model with the image on more than one webpage - but
    also
    let them edit the model on one page without effecting the model on the
    original page - that is why I need multiple copies in the database -
    each
    page points to it’s own image model.
    The problem is that I can not copy the model with the image. I am not
    getting any errors - its just not saving the model. How can I make a
    copy
    of the model with an image?

    Code (this works fine for the models that no not have an image)

    class CreationController < ApplicationController

    def add_copy
    @mod = @user.find_mod(params[:id],params[:type] ) #returns the model
    @mod_copy = (@mod.type).new #creates a new copy of the model
    @mod_copy.attributes = @mod.attributes #gets the models attributes

    this must be the problem because this is where the copy should get the
    image
    if @mod_copy.save #quietly fails on the model with the image

    If you have any suggestions on this issues I would greatly appreciate
    it.

    ActiveRecord::Base#clone does this in a much easier way. Try

    @mod = @user.find_mod(params[:id], params[:type])
    @mod_copy = @mod.clone
    @mod_copy.save

    I just did this at an irb prompt and it worked fine. Try using “save!”
    temporarily instead of “save”. “save!” will raise an error if the
    validation doesn’t pass, giving a bit more insight for debugging.

    “We are all stakeholders in the Karma economy.”

    Totally.


    #11

    Thanks so much, Alex. I will not be able to try anything out until
    Monday -
    but I have high hopes.

    Again thanks for your time and effort - its contributors like you that
    make
    this such a good forum.

    ~K

    On 2/9/07, Alex W. removed_email_address@domain.invalid wrote:

    I have a couple more questions/problems if your up to it.
    app will process it.
    also


    I just did this at an irb prompt and it worked fine. Try using “save!”


    Kim G.
    removed_email_address@domain.invalid

    “We are all stakeholders in the Karma economy.”