In response to your question, the following points can be pointed to:
1- Man has the potential to acquire virtues through performing certain acts that please Allah (swt), and reach a point in which he bears all virtues, becoming a complete individual and achieving total perfection.
2- When one reaches such a rank, it doesn’t mean that he/she won't have different states. According to us and many other Muslims, Imam Ali (as) is the role model of all Muslims who bears the advantage of being a complete individual, nevertheless, he too has different states; meaning that sometimes when praying, he may not feel the pain of pulling an arrow out of his foot, and sometimes in prayer, he hears the call of a beggar and gives his ring in charity for the sake of Allah (swt).
3- What was said in the previous point about the imam (as), implies that his spirituality in the first state he was in (the pulling of the arrow) is much higher than the second one (when he hears the beggar’s voice); but there is much more to this matter than seems; there is a very precise explanation and profound perspective to this matter that must be clarified.
Arefs (those who have reached very high levels of nearness to Allah (swt)) say that one can reach a point in which he bears both the Mulk and Malakut (two worlds and realms, the previous being this world and the latter, having to with the world above and Allah (swt)); meaning that he can be aware of both at the same time; he can pay attention to matters that have to do with above and the Malakut, and simultaneously know of what is going on in this world and the Mulk. It is because of this that arefs consider the second occasion we mentioned regarding the imam (as) (giving the ring to the beggar) higher than the first, although the opposite may come to mind at first sight. The reason being that in this state, the imam (as) was both aware and paying attention to Allah (swt) and other than Him at the same time; meaning that he was both busy with Allah (swt) and with other than Him, not that he stopped paying attention to Allah (swt) and paid attention to the beggar instead, rather, his attention is directed to Allah (swt) so much and completely that he can see what is going on in the entire world at the same time. Therefore this rank is higher than when an arrow is pulled out of his foot in prayer and he doesn’t feel a thing.
As an introduction, we must first say:
1- Man has the potential and capability to acquire certain virtues through performing actions that please Allah (swt), to the extent that one can achieve total perfection and completeness through such.
2- Another point is that the completeness of the imams (as) demands that their lives be like the lives of everyone else; meaning that they have to live in normal and social conditions, and since the conditions of life vary (for instance sometimes there is a state of peace and security in society, and sometimes there isn't), their reactions to different situations vary to meet the needs of each situation in particular (just like how one must pray qasr prayer when travelling, or must pray khawf [fear] prayer in battle; each situation calls for a specific reaction and course of action). Therefore, reaching total perfection and completion doesn’t mean those who have done so aren't allowed to have different states and shouldn’t have anything to do with this world anymore.
Keeping these points in mind, in response to your question, we say that the two occasions mentioned for Imam Ali (as) are two states of the many states a complete individual like the imam (as) has.
In response to this question, Ayatullah Shahid Motahhari says: “Previous scholars have asked the same question about Imam Ali (as) and say: “Whenever Ali would pray, he would become unconscious and totally unaware of everything happening around him, how can you (the Shia) claim that he did such a thing (gave his ring to a needy person in prayer)? The answer is that first of all, it is true that the imam (as) would completely be unaware of everything when praying, but that doesn’t mean that those with such ranks never have different states. Both states have been narrated about the prophet (pbuh) as well; sometimes he would experience absorption to the extent that he couldn’t wait for the adhan to end and would tell Bilal: “ارحنا یا بلال”! “Relieve us, O Bilal! [and finish the adhan quicker so that we can start the prayer]”. On the other hand, sometimes when in sujud (prostration) of the prayer, Imam Hasan (as) or Husein (as) would come and ride his shoulder and the prophet (pbuh) would patiently wait for them to come down before getting up from sujud. One time when the prophet (pbuh) was praying, it is said that someone spat near him; the prophet (pbuh) took two steps forward and covered the spit with his foot [for instance, by moving sand over it with his foot] and returned to his first spot. The fuqaha (Islamic jurists) have used this act of the prophet (pbuh) to derive different fiqh rulings that pertain to prayer. Seyyid Bahrul-Ulum (a prominent Shia scholar) says: “The prophet took two steps forward in prayer and this has solved many of the questions in fiqh pertaining to prayer and how much one can do that has nothing to do with the prayer that won't invalidate it (like taking steps forward and back, or covering something with the foot), therefore, even the prophet (pbuh) has had different states.
In the book of Ilalul-Sharaye’ of Sheikh Saduq (rah), and the book of Biharul-Anwar of Majlisi, it has been narrated: “One day the prophet of Islam (pbuh) was praying and his companions were following him when all of a sudden a baby began to cry. The prophet (pbuh) quickly ended the prayer. After the prayer he was asked why he did such, and he said: “Didn’t you hear the cry of the baby? I ended the prayer quickly so that its mother could come to its relief faster and make it quiet.”
At the same time, it has been narrated that one night, Imam Sajjad (as) was praying when one of his children fell from a height and broke his/her hand and began screaming. All the neighbors gathered around, but the imam (as) was busy worshipping and didn’t become aware of all the noise and what had happened. In the morning, he saw that his child’s hand had been wrapped and asked of the reason and they told him what had happened the night before.
It also has been narrated that the imam (as) was praying and prostrating in his home when a fire started in the corner of the home. The household of the imam (as) screamed: “یابن رسول الله النار النار” (O son of Rasulullah, Fire! Fire!) but the imam (as) continued what he was doing and didn’t become of aware of the fire and the noise. After the fire was put out, the imam (as) rose from his prostration and ended his prayer in total peace of mind.
Therefore, Allah's (swt) apostles and chosen ones have different states. Sometimes they have an average and normal one, and sometimes they are absorbed in the Malakut and Allah (swt) and don’t see anything but him, making them totally unaware of what takes place around them; to the extent that they are even unaware of their own bodies. It is like their senses are no longer intact during divine love and absorption; pulling out an arrow from Imam Ali's (as) foot is a perfect example of this state.
Of course, another answer has also been given to this question:
One of the important advantages of the imams (as) is that in all conditions of life, they are in total absorption of Allah (swt) and don’t seek anything but Him, and the only reason they might pay attention to society and its conditions and what the time and place they live in calls for, is because it is something that pleases Allah (swt) that they use to get closer to Him. They bear both the Mulk and Malakut; if they pay attention to the hereafter, it doesn’t mean that they become unaware of this world, and if they are busy with worldly affairs, it doesn’t mean they forget the hereafter.
It is because of this that according to arefs, the state that Imam Ali (as) was in when he granted his ring to the beggar is higher than the state he was in when an arrow was pulled out of his foot without him feeling anything.
Arefs have mentioned four stages in the spiritual journey to Allah (swt):
1- The journey from creation to the Haqq (Allah (swt))
2- The journey from the Haqq towards the Haqq with the Haqq
3- The journey from the Haqq towards the creation with the Haqq
4- The journey from the creation towards the creation with the Haqq
In the first journey, the one journeying annihilates his essence in the essence of Allah (swt), resulting in his existence becoming “truth-oriented”, this brings a state of disappearance to him (he feels as if he doesn’t even exist). In the second journey, he is annihilated in Allah’s (swt) traits and actions and in the third journey, the state of disappearance goes away and the state of “total awakening and awareness” takes its place. This person becomes immortal by the immortality of Allah (swt) and journeys through the realms of Jabarut, Malakut and Nasut. He witnesses everything in these worlds and informs of Allah's (swt) traits and essence.
In the fourth journey, he reaches a point in which he sees everyone and what they do and what is to their good and bad (both worldly and otherworldly), and sees the return of everything to Allah (swt) and how it all returns to Him. In the three latter journeys, this person is always with the Haqq, because his own essence has become with the Haqq, thus paying attention to anything other than Haqq won't prevent him from remembering it at the same time. Giving the ring to the beggar in prayer is in total compliance with the fourth stage of spiritual journey to Allah (swt).
At the same time, arefs say that when one is attempting to reach perfection, he may reach the level of “comprehensiveness” (those who bear more than one state); this stage can be in one of two ways:
a) Some may have both worlds of Malakut and Mulk completely.
b) Some may have them in an incomplete way, meaning that if they pay attention to what is going on above and in the Malakut, they will become unaware of this world and the Nasut, and that is why when these individuals speak, they sometimes speak in accordance with the state they are currently in and don’t consider everything and all circumstances and may be subject to saying “shatah” things.
Since the imams (as) are infallible, as a result of “لا یشغله شأن عن شأن”, they are with this world when they are with the one above, and are with the world above when they are with this world, and are the manifestation of “عال فی دنوه و دان فی علوه”. To make it short, the imams (as) have both the Malakut and Mulk completely, thus never saying anything that is in contradiction with basic and fundamental beliefs, and in other words, they never say any “shatahs”.
The state that Imam Ali (as) was in when he gave his ring while in ruku, is a manifestation of the rank of completely having both worlds or “complete comprehensiveness”; meaning that when he is paying attention to Allah (swt), he is also completely aware of everything other than Him as well, not that he directs his attention from Allah (swt) to the beggar, rather, he is so into Allah (swt) that he knows of everything else as well.
To put it in even another way, absorption into Allah (swt) is sometimes complete and sometimes incomplete, the latter being when one is absorbed to the extent that he becomes totally unaware of everything else without any awakening.
Ayatollah Shahid Motahhari says: “If absorption becomes complete, that is the stage of return; meaning that the individual is both busy with Allah (swt) and at the same time, with other than Him as well. It is similar to detachment from the body; those who are new to this rank can only achieve it for several seconds or maybe an hour, but there are people who are detached from their bodies in all conditions. For example, they are sitting with me and you, but are detached from their bodies at the same time. The perfect and complete individual, Imam Ali (as), was in this state of total and complete absorption when he gave the ring.
In other words, the complete individual reaches a point in which the whole universe becomes like part of his body. As a result, his existential level and the responsibility that rests on his shoulders calls for him to pay attention to it all, all of this being by virtue of absorption and annihilation in Allah (swt). Therefore, these different states in prayer not only don’t contradict each other, but are in total harmony and confirm each other.
This is the profound explanation to what arefs say about Imam Ali (as) being at a higher level and stage when he gave the ring in prayer in comparison to when an arrow was pulled out of his foot.
 Morteza Motahhari, Emamat va Rahbari (Imamate and Leadership), pp. 180-181 (with a little alteration).
 Biharul-Anwar, vol. 88, pg. 93.
 Muntahal-Amal, vol. 2, pg. 10.
 Like the story of Prophet Yaqub (pbuh) and Prophet Yusuf (pbuh); Yusuf was thrown into the well out of his brothers’ jealousy, but Prophet Yaqub didn’t learn of this incident. After many years, Yusuf became a high-ranking official in Egypt. His brothers went to him and brought back his shirt to their father. The Quran says: “و لما فصلت العیر قال أبوهم إنی لأجد ریح یوسف” (Yusuf:94) (When the caravan of Yaqub’s sons began to move from Egypt, Yaqub [who was in Kan’an/Canaan] said: “I smell the scent of Yusuf.”)
Sa’di, the famous Persian poet has said some things regarding this matter and how Prophet Yaqub didn’t find out that Yusuf had been thrown into the well, but could smell the scent of Yusuf from that far away and that it has to do with the different states prophets are in: Koliyyate Sa’di, pg. 53.
 Mohammad Reza Hakim Elahi Qomshe’I Esfehani, Hashiyeye Asfar, vol. 1, pp. 13-16 (shortened).
 Definition of Shatah: The amount of content that spills out of a pot when boiling and also a word that is used to shoo off a one-year baby goat, also it means to violate rules. Mo’in and Dehkhoda Dictionaries.
In gnosis and mysticism, this term refers to sayings which apparently are in contradiction with Islamic fundamentals that arefs and mystics may sometimes say when they have reached high levels of spirituality and unconsciousness and happiness. When saying these things, they are totally unaware of themselves and what is going on and are totally absorbed in Allah (swt). The words they say that apparently is in contradiction with Islam (words that religious people will never even think of saying) can be justified and explained in a way that they will no longer be in contradiction.
 Translation of Risalatul-Wilayah, the saying of Ayatullah Javadi Amoli.
 Morteza Motahhari, Emamat va Rahbari (Imamate and Leadership), pg. 181.