On Jan 16, 2009, at 2:44 PM, Michael Libby wrote:
> On Fri, Jan 16, 2009 at 1:21 PM, naevity
> <[email protected]> wrote:
>> yes, hardcoding it works. Let's say I want to use an ISBN barcode of
>> "068816112X"
>>
>> This works:
>>
>> <%= barcode '068816112X', :encoding_function =>
>> Gbarcode::BARCODE_ISBN
>> %>
>
> Good. Just checking that the easy case works. :)
>
>> Now, let's say in my types table, I have this:
>>
>> id: 1
>> type: book
>> upctype: GBarcode::BARCODE_ISBN
>
> I think the problem is your model then.
>
> GBarcode::BARCODE_ISBN is not a string. It's a constant that stands
> for the Fixnum 3.
>
> irb(main):001:0> require 'rubygems'
> => true
> irb(main):002:0> require 'gbarcode'
> => true
> irb(main):003:0> Gbarcode::BARCODE_ISBN
> => 3
> irb(main):006:0> Gbarcode::BARCODE_ISBN.class
> => Fixnum
>
>> <%= barcode item.upcnumber, :encoding_format => item.type.upctype %>
>>
>> but that's when I get the "in method 'Barcode_Encode', argument 2 of
>> type 'int' " error.
>
> It's a somewhat opaque error message, but it's telling you that the
> second argument is the wrong type... and that it wants an int.
>
> Your view could just as easily contain:
>
> <%= barcode "some_string", :encoding_format => 3 %>
>
> and it should work the same as
>
> <%= barcode "some_string", :encoding_format =>
> Gbarcode::BARCODE_ISBN %>
>
> The value you need to store in your database is the integer/Fixnum.
>
> -Michael
>
> --
> Michael C. Libby
> www.mikelibby.com
You can probably leverage ActiveSupport#constantize
class Item
def enc_type
self.type.upctype.constantize
rescue NameError
Gbarcode::DEFAULT
end
end
assuming that you *want* to store Gbarcode::BARCODE_ISBN in the
database rather than its value as a Fixnum.
HOWEVER, I'll warn you that the name 'type' is reserved by
ActiveRecord for single-table inheritance and you might be better-off
using a word like 'kind' or 'format' ;-)
-Rob
Rob Biedenharn http://agileconsultingllc.com
[email protected]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---