我希望能夠「PARAMS」傳遞給「is_following」的方法如下:添加PARAMS渲染方法
respond_to |format|
format.json { render json: @user, :methods => [:is_following params] }
end
然而,當我把「PARAMS」方法名稱後,它會引發一個錯誤:
syntax error, unexpected tIDENTIFIER, expecting ']'
。
。
當我呈現JSON(不is_following法),它看起來像這樣:
[{"id":81,"image_url":"https://graph.facebook.com/123213123/picture?type=large","full_name":"John Johnson"},{"id":85,"image_url":"https://graph.facebook.com/123123123/picture?type=large","full_name":"Bill Nye"}]
我想補充的is_following方法,以便JSON洛斯這樣的:
[{"id":81,"image_url":"https://graph.facebook.com/123213123/picture?type=large","full_name":"John Johnson",'is_following'=>4},{"id":85,"image_url":"https://graph.facebook.com/123123123/picture?type=large","full_name":"Bill Nye",'is_following'=>9}]
編輯
def friends
@user = User.first
respond_to do |format|
format.json { render json: @user.friends.order("first_name ASC"), :methods => [:is_following], :only => [:id] }
end
end
和is_following方法模式是:
def is_following params
return Friendship.where("user_id = ? AND friend_id = ?", params[:user_id], self.id).count
end
有誰知道如何解決這個問題?我會給你一百萬美元。
語法錯誤告訴你在':is_following'之後缺少逗號。 – jdoe 2012-04-17 18:16:11