【问题标题】:RAILS: how to pass JSON API data to hash in controllerRAILS:如何将 JSON API 数据传递给控制器​​中的哈希
【发布时间】:2017-01-06 19:32:09
【问题描述】:

当我将它放在我的帮助文件中时,我可以将此 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"] 上的 nil:NilClass 的未定义方法 `[]',

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

【问题讨论】:

    标签: ruby-on-rails json hash instagram-api endpoint


    【解决方案1】:
    "pict" => file["data"][record_number]["images"]["standard_resolution"]["url"],
    

    这个链中的某个值是nil,这是导致

    InstagramSessionsController#index 中的 NoMethodError 未定义方法 `[]' 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
    

    【讨论】:

    • 感谢 broham wil chekc 它是我们的。
    • 像个魅力兄弟一样工作。
    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 2018-07-03
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多