-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use JsBarcode with Stimulus and importmaps
- Loading branch information
Showing
4 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
import JsBarcode from 'jsbarcode'; | ||
|
||
// Connects to data-controller="barcode" | ||
export default class extends Controller { | ||
connect() { | ||
const options = { | ||
format: "CODE128", | ||
// format: "CODE39", | ||
lineColor: "#0000FF", | ||
displayValue: false, | ||
width: 2, | ||
margin: 0 | ||
} | ||
JsBarcode(this.element, this.element.dataset.code, options) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
class Product < ApplicationRecord | ||
# after_create do | ||
# update(barcode: SecureRandom.hex) | ||
# end | ||
|
||
before_create do | ||
self.barcode = SecureRandom.hex | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ | |
<%= product.barcode %> | ||
</p> | ||
|
||
<img data-controller="barcode" data-code="<%= product.barcode %>"> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters