Currently I am using the Last.fm api to return concert data (returns a hash) in my controller, and in the view cycling through this hash to return the data I want. I want this concert data to become more dynamic and put everything into a model. How do I do this? Should I do this in the controller or somehow in the model?
Here is an example of my code
# app/controllers/events_controller.rb
class EventsController < ApplicationController
def index
@events = @lastfm.geo.get_events("Chicago",0,5)
respond_with @events
end
end
# app/views/events/index.html.erb
<% @events.each do |event| %>
Headliner: <%= event["artists"]["headliner"] %>
<% end %>
In this example I would would want and Event Model with headliner as a parameter, and put all 5 of the events into this model.
I believe it’s a good idea to have a model. There are several advantages as I can see
1 – You could access data as OO way as other objects
2 – if you have some business logics (Ex: calculations) you could do it in the model itself with out messing up your view
3 – it’s clean and DRY
Example model class would be (this is not a working model, but just to give you an idea):
So you can clean-up your view as