RAILS: 如何将 JSON API 数据传递给控制器​​中的散列

RAILS: how to pass JSON API data to hash in controller

当我将它放入我的帮助文件中时,我可以将这个 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(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

^^ 我收到错误消息:

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"],

我收到错误 InstagramSessionsController#index 中的 NoMethodError "pict" => 文件["data"][record_number]["images"]["standard_resolution"]["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
"pict" => file["data"][record_number]["images"]["standard_resolution"]["url"],

此链中的某些值是 nil,这导致

NoMethodError in InstagramSessionsController#index undefined method `[]' for nil:NilClass on the "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