I'm going to the Rockies (trails in Jasper and Banff). Would it be safe for me to cook food while on the trail? Won't bears be attracted towards the smell of the food?
I think part of the answer to your question relates to WHERE in the Rockies you'll be visiting. Will you be in an area that is not visited much by humans, or will you be visiting a high-traffic area such as a national park or other popular tourist destination?
If you will be hiking through an area that has few or sporadic hikers, then you really have nothing to worry about. Black bears, in most of the mountain west, are not accustomed to humans and their fear of man is firmly intact - especially in areas where they are hunted a little each year. They do an excellent job at staying away from humans, no matter how yummy we might smell. So fire up your stove and cook whatever you want. Now, that being said, do take precautions. Clean up after yourself. Don't leave garbage in the woods. Hang your food at least 100 feet away from your campsite. (A few tiny bread crumbs on the ground won't matter; the mice, birds, and chipmunks will clean them up pretty quick.) You'd hate to acclimatize a bear to humans and have it become a problem to others. Someone, the bear included, could end up getting hurt.
I have spent a LOT of time in areas that have plenty of bears but not a lot of people: backpacking, hunting, fishing, dayhikes. Sometimes I ate food that I'd prepared at home, while other times, I cooked in the mountains. I have NEVER had a bear problem.
On the other hand, I have not spent much time in areas with lots of visitors. In these areas, I would still cook my food, but I would be more cautious. Bacon and foods that have tons of sugar in them are especially enticing to bears. Go ahead and cook them, but then get them cleaned up ASAP. NEVER store your food in your tent. Be serious about hanging it at least 10 feet above the ground and more than 4 feet from any tree trunks. Better yet, use a bear resistant container, if you can afford it. Some campsites in high traffic areas have metal, bear-proof food containers. Use them. Especially in areas that might have grizzlies - anywhere near the borders of Idaho/Montana, Idaho/Wyoming, and Montana/Wyoming.
As an extra precaution, carry a canister of bear spray in a spot that is accessible - not inside your backpack. Be sure you know how to use it.
Many people travel for days or weeks in bear country. Several existing questions on this site address bear and camp/food related issues. If you've never traveled in areas with bears before, you should definitely give these a read:
It is recommended to cook and store at least 200 feet away from where you sleep. Assuming you'll be moving on after eating, as you asked "on the trail", then as long as you aren't stopping to eat in the middle of a frequently used camping location it should be fine. If actually cooking, as in pulling out your stove and not just eating a snack out of a bag, you should also be at least 200 feet from water and trails to minimize impact and follow Leave No Trace principles.
Bears are attracted to food, and you do need to take special precautions when you stop to camp to protect your food and the bears from each other. There are both legal requirements, such as use of bear-proof storage containers, as well as best practices, which depend on your location. If you want to know more about the specifics for the area you're visiting, I would suggest calling the relevant land management agency for wherever you'll be - e.g. the Park Service, Forest Service, etc.
I'll add to the existing answers by saying that the time of year likely does matter. I've been the Canadian Rockies up by Banff and some of those trails are heavily traveled. But, during the berry season (autumn), that doesn't stop bears from coming around even when you're not cooking something delicious. If you talk to folks who have hiked up there a lot, they may have stories of close encounters.
If you're hiking around trails that are frequently used, you should cook far away from the trails so as not to unnecessarily attract bears to the trails. If they are out roaming about in these areas (e.g. during berry season) they already exhibit some brash behavior and I would be careful about enticing them.
It is important to never let a bear get any of your food. Do not cook and eat in the open outside your shelter. You would be foolish to try to defend your food in the open. You can cure a bear from trying to steal any camper’’s food if you cook and eat inside and give him a good dose of pepper spray. Your shelter should have a peek out vent on every side. Every side should be free to allow a hand with the spray can to be pushed under to get an accurate shot of spray in the bears face.