I’ve been helping a local store developing a Point of Sale (POS) system recently, and one of their requirements is printing price tag from within the product management interface. The first time I heard about this, I thought it was impossbile to have a web app to talk to a local printer. “There must be something like a client app, which installed on the client side, retriving the print jobs via a REST api and send it to the label printer.”, my initial thought leads me to start looking for Java Applet, which pretty much acts as a client app but residing in a web page.
After few google searches, I found a Java Applet named jZebra, which looks pretty much what I need, and it’s now moved to to QZ.
Download and install the free version of the software, now I have QZ-Tray as a broker who’s going to talk to the printer, the applet in the web page is going to communicate with QZ-Tray via WebSocket, it works like a magic, but there’re few pitfalls I need to mention here hoping it could help someone like me.
setup the label printer
QZ has provided a really great tutorial on how to setup the label printer, I tried to follow the instructions, but never succeeded. The thing is from within http://localhost:631/admin, you should not click “Add Printer”, while you should click “Find New Printers”, and follow the rest of the instructions from QZ. You may need to restart the QZ-Tray to pick up the changes.
A problem I run into recently is I am trying to send an AJAX request to fetch grouped options for a select input, the select input is dynamically generated, and I don’t want to assign an unique random ID to it, and pass the ID to the server as the DOM selector, I do believe there’s a better way of doing this, here’s what I come up:
As you can see, the key to retrieve the variable groupedOptionsHTML is to evaluate the data returned using eval(data). Please drop a line if you have any better idea to solve this problem, cheers.
Believe it or not, it’s been 6 years since the ticket was created about “Rails doesn’t track in place changes to serialized column”, it’s really a tricky problem rare people will run into. Consider the following scenario:
As commented by Jose Valim link, it’s a feature of Rails and it’s documented somewhere. The workaround is issue another options_will_change! to mark the object changed.
It gets more tricky especially when you are using accepts_nested_attributes_for, the Rails will ditch the changes to the children objects if changed? returns false.
# spec/controllers/questions_controller_spec.rbdescribeQuestionsControllerdodescribe"PUT /update"doit"updates the question with its answers"doq1=Question.createa1=Answer.create(question:q1,options:[1,2,3])put:update,id:q1.id,question:{answers_attributes:{"0"=>{id:a1.id,options:[1,2,3,4]}}}a1.reloadexpect(a1.options).toeq[1,2,3,4]# FAIL!!endendend
It really got me and hope this will help somebody like me, happy hacking!!
I’ve been playing with Rails 4 recently, the strong parameter, as you all know, is a new feature just appeared in Rails 4. It will get really tricky when you are posting a nested form.
The above code are really straightforward Rails 4 code for updating a given question with its answer, the problem right now is every time I updated the question, no matter if updated the associated answer or not, it will destroy the old answer object, and create a new answer object for me, that’s really annoying, the gotcha here is you need have the answer’s id field go through for strong parameter, otherwise, it will be filtered out, and the old answer will be destroyed, and a new answer object will be created silently. The revision is something like:
@flyerhzm has done a great job on when you should use after_commmit instead of after_save in this post. One thing is sometimes you may want to check if there’s any changes to a specific column before executing the code within the callback. Sadly, since it is in after_commit block, all the changes have been persisted into the database, you will never got a chance to check if there’s any changes to any columns, that’s the problem we are going to attack.
Obviously, you don’t want to bother the police every time you change your hair style is changed, believe me, you don’t want to do that. The fact is we need to let the police know once you change your name or your DOB. You will find a tricky bug in the above code, the PoliceNotification.notify(self) will never get triggered because in the after_commit block, you will never see any changes to any columns. So, the way I fix this problem is:
As you can see, I set a flag @name_or_dob_changed within the after_save block, as long as you do not reload the object, you can do the check based on the flag in the after_commit even after all the changes are persisted into database.
It’s been 12 years we know each other since 2002, I can never forget that summary vacation when I saw you, I didn’t know that was love.
2009, we got married, for the past 12 years, we take care of each other, we fight, sometimes we went too far to say the word “divorce”, we were too young.
2013, our angel baby girl came to this world, that’s the best thing I’ve had in my life, I can not forget the night when you are going to deliver, you woke me up gently and calmed me down. The moment you were sent into the delivery room, I had a lot in my mind, it was a rainy night, a peaceful night. When our baby was out, and you were sent to the ward, I can still remember the blood all over you, I can not help myself, you are a hero mom, to me and to our baby.
I am the most happy man in world I have you in my life, let’s expore this wonderful world together, never separated.
You may have run into the same situation with me, given a Chinese character, you want to find out the corresponding Chinese Telegraph Code, there’re tons of utility tools online for that, but the thing is you want to do it on the server side, here’s a gem I am going to present to you: ctc, check it out and convert the Kanjie to CTC and vice versa like a boss, :)
One thing I love Rails is it provides really handy URL helpers to the developers. Today, I am going to present you guys how to use polymorphic_url like a ninja.
As you can see, we’ve have a STI table named vehicles in the database, and it stores all the bicycles, motorcycles and the other new vehicles we may have in the future, like unicycle.
We have a collection of vehicles, @vehicles, it contains bicycles and motercycles. What we are going to do is iterating the @vehicles, and display a link to the vehicle detail page. With polymorphic_url, we can write down the following code:
<% @vehicles.each do|v|%> <% if v.is_a? Bicycle %><%= link_to "Vehicle Detail", bicycle_page(v) %> <% elsif v.is_a? Motercycle %> <%=link_to"Vehicle Detail",motorcycle_page(v)%> <% end %><% end %>
Once we add a new kind of vehicle, we don’t need to open the above view file and add another elsif, it complies to the Open Closed Principle perfectly!
What if we want to have the same controller rendering the same view for all the vehicles? Say, we only want one VehiclesController#show for all the vehicles’s detail, here we go:
You may say we can just use vehicle_url(v) to generate the URL, I can not agree more, but we are exploring something deep inside of Ruby on Rails, so, bare with me, :)
To have the polymorphic_url, generate the URL like vechicle_url, we need to overwrite self.model_name for the Vehicle, here’s how I found it out:
the polymorphic_url will call build_named_route_call to generate the URL
the build_named_route_call will call RecordIdentifier.__send__("plural_class_name", record) to find out reource name, say, a record of Bicycle will generate bicycles
the plural_class_name will call model_name_from_record_or_class to determine the model name of the record
model_name_from_record_or_class will call the record’s class model_name method to find out the model name of the class
We need to override the self.model_name to let polymorphic_url pick up the right model name:
You are working on a Rails 3 project and your client asks you to sort a list of items and display the items in order, the first thing come to your mind might be acts_as_list and jQuery sortable, you are not alone. I found that acts_as_list requires the items to be sorted belong to a parent, sadly, that’s not the case I am facing, the items I am trying to sort belongs to nothing, RankedModel come to rescue.
ranked-model is a modern row sorting library built for Rails 3 & 4. It uses ARel aggressively and is better optimized than most other libraries.
Lucky me, i found this great post introducing how to get RankdedModel work with Bootstrap table and jQuery sortable.
The requirement I am facing is bit more complex than the one explained in the above article, I have a list of items, which is an array of items of different types stored in one single tables, say, I have the following object layout:
Say, I have a list of employees, including managers, salesman shown in order, I can now drag and drop to sort the items in the list, great, ready to charge the customer? Not yet, you will find it will only sort the managers and salesmen separately, it will not sort them as a whole entity, dig into the source of RankedModel, i found something really interesting,
This where the RankedModel decides which class to use as the finder, the problem it could not sort managers and salesmen as a whole entity is because it will use Manager and Salesman separately to do the sorting, RankedModel found this problem before I do, and they provide an option class_name to unify the finder lookup, the solution is: