I am new here and really new to Ruby so I will do my best to ask a good question. Basically I am trying to write an app that returns Facebook events a user has been invited to. Pretty simple. The issue is it keeps returning everything like this
[{"name"=>"SPB Presents An Evening With Demetri Martin"}], [{"name"=>"Say \"Pi\" to Passover!"}]
I just want the value of the key. I've tried to use ["name"] and look at basic Ruby tutorial but I got a myriad of errors each time.
Here is my code:
HomeController:
def index
if session["fb_access_token"].present?
@fql = Koala::Facebook::API.new(session["fb_access_token"])
@invites = @fql.fql_query("SELECT eid FROM event_member WHERE uid = me()")
end
def names(eid)
if session["fb_access_token"].present?
@fql = Koala::Facebook::API.new(session["fb_access_token"])
@fql.fql_query("SELECT name FROM event WHERE eid = #{eid}")
end
end
Home View
<% if @invites %>
<% for invite in @invites %>
<p><%=h names(h invite["eid"])%></p>
<% end >
<% end >
Thank you so much in advance for your help! Also if anyone has a better way to build this so I don't have to do so much work in the views or how to do a better for each loop in the controller, that'd be nice too!
I figured out my problem if anyone is curious. The fql query stores everything in an array with one hash of "location" in it, so each time I displayed the query it displayed the array. This was my fix:
def names(eid)
if session["fb_access_token"].present?
@fql = Koala::Facebook::API.new(session["fb_access_token"])
@hashmap = @fql.fql_query("SELECT name FROM event WHERE eid = #{eid}")
@name = @hashmap[0]["name"]
Now I pull the first object in the array at index 0 and the use the key "name." Worked like a charm!