Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:13:06+00:00 2026-06-09T08:13:06+00:00

I have got myself quite befuddled. I have some code which takes some images,

  • 0

I have got myself quite befuddled. I have some code which takes some images, combines them and then spits out the combined image in .png format.

Originally this code was a method for a model – with the model’s associations indicating which images to use. Thus:

class Component < Refinery::Core::BaseModel  
    drawing_accessor :drawing
  . . .
end

class Photo < Refinery::Core::BaseModel
  has_and_belongs_to_many :components
  has_many :drawings, :through=>:components

  def diagram
    . . . .
    Base64.encode64(png.to_blob)    #spit out the png as a base64 encoded string
  end
end

and in a view I could write

  <img src="data:image/png;base64,<%=@photo.diagram%>"

Now, I need to do the same combining of images, but directly from a list of component ids. As the component ids haven’t been saved to a photo (and may not be) I need to move this code out of the photo model.

I want to able to call the same drawing code with a parameter that is a list (array or collection) of component ids, regardless of where they come from.

It seems that as the diagram comes from a set of components, it should belong with the components…somewhere.

In my various tries I end up with undefined method for an ActiveRecord::Relation, or for an Array.

Can you help clarify my thoughts about where this code belongs and how to call it?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:13:08+00:00Added an answer on June 9, 2026 at 8:13 am

    Well, the Power of Posting has hit again.

    I put in a new route for the components collection:

      resources :components do
        collection do
          get :draw
        end
      end
    

    with a matching definition in the controller

    def draw                 
      send_data Component.construct(params[:list],params[:width], params[:height]), :type => 'image/png', :disposition => 'inline'
    end  
    

    and a method on the model to draw the components

      def self.construct(component_list, width, height)
      . . . 
        Base64.encode64(png.to_blob)    #spit out the png as a base64 encoded string
      end 
    

    The Photo model includes a method which pulls together the component list then call construct:

      def diagram
        component_list = []
        # construct the list of ids in the right order (bottom to top, or base to capital)
        ....
        Component.construct(component_list, self.image.width, self.image.height)
      end
    

    And from javascript I can call

    var component_list = $("input:checked").map(function(){return this.value}).get();
    . . . 
    $.get(url,{list:component_list, width:width, height:height}, function(data) {
      $('img.drawing').attr("src","data:image/png;base64," + data);
    })
    

    I still have doubts about including the methods in the models and not somewhere in a view or view helper, but this does seem to work!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code and got myself confused: I have a query that
Okay, this is quite an interesting challenge I have got myself into. My RegEx
I have some experience with XSLT but now i've got myself a problem: I
I have got myself saddled with an existing application - which attempts do do
I have got myself into a bit of a mess trying figure out the
Have got an NSString *str = @12345.6789 and want to find out if there
I have got this code: function init(){ if (typeof window.jQuery !== 'function') { var
I have got a form into which information is entered, and a drop down
I have got a table [newsletter] in the db, which saves the email address,
I have got a few tables which I am trying to join. I just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.