Alumatris
Alumatris

Reputation: 15

Where do I put ruby code that I want to run when users access certain pages?

I have a bit of code that I need to run when a user loads a page. What it does specifically is update my database based on an xml file using a gem. So far I've found answers that tell me I should put it in everything from a rake task to the lib folder itself to the model. It's all a little confusing.

Here's the code in question:

require 'rubygems'
require 'eaal'
EAAL.cache = EAAL::Cache::FileCache.new 
api = EAAL::API.new("id", "vcode", "char")                                  
result = api.MarketOrders("characterID" => "id")
result.orders.each do |order|
  @found = MarketItem.find_by_typeid(order.typeID.to_i)
  MarketItem.update(@found.id, :remaining => order.volRemaining.to_i)
end

I'm sorry if this is an obvious question and I'm sure my code is horrible. I'm really new to rails and the only way I seem to be able to learn new languages is the bull-in-a-china-shop method.

Upvotes: 0

Views: 56

Answers (1)

Sully
Sully

Reputation: 14943

There is a route to that page that hits a function first

for example. show_user_path will hit the function show. You can put your code at the beginning of the function that renders the page.

Upvotes: 2

Related Questions