On Fri, Jan 16, 2009 at 5:21 PM, naevity <[email protected]> wrote: > > > > On Jan 16, 1:52 pm, "Michael Libby" <[email protected]> wrote: >> On Fri, Jan 16, 2009 at 12:32 PM, naevity <[email protected]> wrote: >> > <%= barcode 'UPCCODE', :encoding_function => Gbarcode::BARCODE_ISBN %> >> >> Hard-coding it like this works for you, right? >> >> > I need to be able to dynamically input the encoding function like >> > this: >> >> > <%= barcode code.number, :encoding_function => code.type.upctype %> >> >> What are some sample values of code.type.upctype from your database? >> >> -Michael > > > > > 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 > %> > > Now, let's say in my types table, I have this: > > id: 1 > type: book > upctype: GBarcode::BARCODE_ISBN > > id: 2 > type: dvd > upctype: GBarcode::BARCODE_128 > > > and in my 'items' table (instead of code, works better for the > example) I have this: > > id: 1 > type_id: 1 > name: replay > upcnumber: 068816112X > > id: 2 > type_id: 2 > name: batman > upcnumber: 123456789 > > I can do this without a problem: > > <%= barcode item.upcnumber, :encoding_format => Gbarcode::BARCODE_ISBN > %> > > But that would obviously make the next entry of Batman show up wrong, > since it needs to be a BARCODE type of 128. > > This is where stuff start's going wrong. I thought I should be able to > enter the below without a problem > > <%= 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. > > > If you want to take a look at the plugin's code where the > Barcode_Encode method lies, it's here: > > http://code.google.com/p/barcode-generator/source/browse/trunk/barcode_generator/lib/barcode_generator.rb > > I really appreciate the help, I've wasted an entire day trying to wrap > my head around it. It seems like it should work to me, but I obviously > don't know enough as to why it's not working.
It won't work the way you are doing, because "Gbarcode::BARCODE_ISBN" is ruby code (It's a constant from Gbarcode class) and needs to be evaluated and when you do "item.type.upctype" the result is the STRING "Gbarcode::BARCODE_ISBN" which is not interpreted, therefore not evaluated as the constant. This code: <%= barcode item.upcnumber, :encoding_format => eval item.type.upctype %> works, but it's dangerous and you should avoid it. > > > -- cheers, Gustavo Sacomoto Vice-gerente Geral Qype Brasil +55 (11) 76747726 www.qype.com.br --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

