Ruby on rails RAILS:如何将JSON API数据传递到控制器中的散列
当我把它放在works的助手文件中,我可以将这个API信息读入散列。但这在我的控制器中不起作用。我需要它在我的控制器中工作,以便分页Ruby on rails RAILS:如何将JSON API数据传递到控制器中的散列,ruby-on-rails,json,hash,instagram-api,endpoint,Ruby On Rails,Json,Hash,Instagram Api,Endpoint,当我把它放在works的助手文件中,我可以将这个API信息读入散列。但这在我的控制器中不起作用。我需要它在我的控制器中工作,以便分页 def object JSON.parse open(end_point).read end def feed # <- feedjust a naming function for object file = object # return file # end def photo_object
def object
JSON.parse open(end_point).read
end
def feed # <- feedjust a naming function for object
file = object #
return file #
end
def photo_object(object, record_number) # <- should take a feed or object
output = Hash.new
output = {
"pict" => file["data"][record_number]["images"]["standard_resolution"]["url"],
"height" => file["data"][record_number]["images"]["standard_resolution"]["height"],
"width" => file["data"][record_number]["images"]["standard_resolution"]["width"],
"name" => file["data"][record_number]["user"]["full_name"],
"profile_pict_Instagram" => file["data"][record_number]["user"]["profile_picture"],
"likes" => file["data"][record_number]["likes"]["count"],
"film_look" => file["data"][record_number]["filter"],
"post_date" => Time.at((file["data"][record_number]["created_time"]).to_i),
"rights" => file["data"][record_number]["attribution"],
"tags" => file["data"][record_number]["tags"]
}
return output
end
我得到了错误
InstagramSessionsController索引中的命名错误
未定义的nil方法“[]”:在文件[“数据”][记录编号][“图像”][“标准分辨率”][“url”],
def show
end
def new
@instagram_session = InstagramSession.new
end
def edit
end
# POST /instagram_sessions
# POST /instagram_sessions.json
def create
@instagram_session = InstagramSession.new(instagram_session_params)
respond_to do |format|
if @instagram_session.save
format.html { redirect_to @instagram_session, notice: 'Instagram session was successfully created.' }
format.json { render :show, status: :created, location: @instagram_session }
else
format.html { render :new }
format.json { render json: @instagram_session.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /instagram_sessions/1
# PATCH/PUT /instagram_sessions/1.json
def update
respond_to do |format|
if @instagram_session.update(instagram_session_params)
format.html { redirect_to @instagram_session, notice: 'Instagram session was successfully updated.' }
format.json { render :show, status: :ok, location: @instagram_session }
else
format.html { render :edit }
format.json { render json: @instagram_session.errors, status: :unprocessable_entity }
end
end
end
# DELETE /instagram_sessions/1
# DELETE /instagram_sessions/1.json
def destroy
@instagram_session.destroy
respond_to do |format|
format.html { redirect_to instagram_sessions_url, notice: 'Instagram session was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_instagram_session
@instagram_session = InstagramSession.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def instagram_session_params
params.require(:instagram_session).permit(:layout)
end
end
此链中的某些值为nil
,这导致
InstagramSessionsController中的NoMethodError#索引未定义的方法“[]”,用于“pict”=>文件[“数据”][记录编号][“图像”][“标准分辨率”][“url”]上的nil:NilClass
为了避免如此多的链接,您可以稍微更改方法,以避免此类错误
def photo_object(object, record_number) # <- should take a feed or object
data = file["data"] && file["data"][record_number]
return {} unless data.present?
images = data["images"] && data["images"]["standard_resolution"]
user = data["user"]
output = {
"pict" => images && images["url"],
"height" => images && images["height"],
"width" => images && images["width"],
"name" => user && user["full_name"],
"profile_pict_Instagram" => user && user["profile_picture"],
"likes" => data["likes"] && data["likes"]["count"],
"film_look" => data["filter"],
"post_date" => Time.at((data["created_time"]).to_i),
"rights" => data["attribution"],
"tags" => data["tags"]
}
end
def photo_object(object,record_number)#图像和图像[“url”],
“高度”=>图像和图像[“高度”],
“宽度”=>图像和图像[“宽度”],
“名称”=>user&&user[“全名”],
“profile_pict_Instagram”=>用户和用户[“profile_picture”],
“likes”=>data[“likes”]&&data[“likes”][“count”],
“film_look”=>数据[“filter”],
“发布日期”=>Time.at((数据[“创建时间”])到i),
“权限”=>数据[“属性”],
“标记”=>数据[“标记”]
}
结束
谢谢broham wil chekc。我们的工作就像一个迷人的兄弟。
"pict" => file["data"][record_number]["images"]["standard_resolution"]["url"],
def photo_object(object, record_number) # <- should take a feed or object
data = file["data"] && file["data"][record_number]
return {} unless data.present?
images = data["images"] && data["images"]["standard_resolution"]
user = data["user"]
output = {
"pict" => images && images["url"],
"height" => images && images["height"],
"width" => images && images["width"],
"name" => user && user["full_name"],
"profile_pict_Instagram" => user && user["profile_picture"],
"likes" => data["likes"] && data["likes"]["count"],
"film_look" => data["filter"],
"post_date" => Time.at((data["created_time"]).to_i),
"rights" => data["attribution"],
"tags" => data["tags"]
}
end