ruby on rails - How do I put API data into a model? -
currently using last.fm api return concert data (returns hash) in controller, , in view cycling through hash return data want. want concert data become more dynamic , put model. how do this? should in controller or somehow in model?
here example of 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 |event| %> headliner: <%= event["artists"]["headliner"] %> <% end %>
in example would want , event model headliner parameter, , put 5 of events model.
i believe idea have model. there several advantages can see
1 - access data oo way other objects
2 - if have business logics (ex: calculations) in model out messing view
3 - clean , dry
example model class (this not working model, give u idea :))
class event attr_accessor :headliner def self.event_list(limit = 5) lastfm.geo.get_events("chicago",0,limit) end end
so can clean-up view
<% event.each |event| %> headliner: event.headliner <% end %>
i think u point :)
Comments
Post a Comment