开发者

Basic question about how to call a method

I really should know this... but I don't. Here's is a method provided in the do开发者_JAVA技巧cumentation for a gem, Barby:

valid?()

And here is a functioning callback in my model:

  require 'barby'
  require 'barby/barcode/bookland'

def barcode

  if Barcodeimg.find_by_isbn_id(self.id).nil?
     barcode = Barby::Bookland.new("'#{self.productidentifier_idvalue}'")
     my_bc = Barcodeimg.new(:client_id => self.client_id, :isbn_id => self.id) 
     f = File.open("barcode.png", 'w+')
     f.write barcode.to_png
     my_bc.image = f
     my_bc.save!
  end
end 

However, sometimes I get a 'data not valid' error, so I'd like, obviously, to use the valid? method. But I don't know how to call it. If I do

  if self.productidentifier_idvalue.valid? 

I get

undefined method `valid?' for "9781551113180":String

Edit: more errors:

If I do

barcode = Barby::Bookland.new("'#{self.productidentifier_idvalue}'")
if barcode.valid?
#more

I get the Barby error 'data not valid'


If you're giving an invalid ISBN here

barcode = Barby::Bookland.new("'#{self.productidentifier_idvalue}'")

it's no wonder it gives you the data not valid error. Check the value of self.productidentifier_idvalue and make sure it's 12 digits (the regexp to validate the code can be seen here).

Update

Replace "'#{self.productidentifier_idvalue}'" with self.productidentifier_idvalue. The way you're doing it, the result of your expression is '123456789012', which is two quotes too many.

Besides, you don't really need to call valid?, it's done in the initializer as you can see here.


According to the exception, you're calling this on a string, which doesn't know that method. You need to call it on wherever your Barby::Barcode instance is.

According to the documentation "Bookland" is a special case of "pure" EAN-13 data.

So, maybe doing something like

bc = Barby::EAN13.new(productidentifier_idvalue)
bc.valid?

Should get you closer to your aim


The valid? method exists only on objects which are instances of the Barcode class

You're calling this method on a String.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜