【问题标题】:Rendering Paperclip URL's using JBUILDER使用 JBUILDER 渲染回形针 URL
【发布时间】:2016-06-26 03:25:34
【问题描述】:

我正在使用 Ruby on Rails 构建一个 API。

我有使用回形针实现头像的用户。

我试图在我的 JSON 输出中访问我的回形针 URL,但它刚刚使我的 heroku 实例崩溃。这在本地完美运行。

我的用户模型片段

class User < ActiveRecord::Base

  has_attached_file :avatar, styles: { large: "600x600#", medium: "300x300#", thumb: "100x100#" }, default_url: "https://s3.amazonaws.com/whiztutor-marketing/photos/Profile300.png"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

end

我的用户的 Jbuilder 片段

json.cache! @user do
  json.id @user.id
  json.type @user.type
  json.f_name @user.f_name
  json.about @user.about
  json.email @user.email
  json.avatar_url @user.avatar.url(:medium)
  json.referral_code @user.referral_code
  json.education_level @user.education_level
  json.photo_url @user.avatar.to_json
  json.education_details @user.education_details.all
  json.all_subjects @user.subjects.all
  json.all_times @user.all_available_times.each do |time|
    json.day time.schedule.to_s
    json.time_block time.time_as_string
    json.next_occurrence time.schedule.next_occurrence(Time.now)
  end 
end

我尝试将其包装到this question 上的方法中,它以完全相同的方式破坏了服务器。我什至可以运行控制台并直接通过服务器访问这些 URL。 JBUILDER 和 PAPERCLIP 的一些东西不能混合在一起,我似乎无法深入了解它。任何帮助是极大的赞赏。

【问题讨论】:

  • 你能发布你在 Heroku 上看到的错误消息吗?
  • 服务器上带有回形针的 aws 凭证配置可能存在问题。你能告诉我们错误信息和回溯吗?
  • 我的日志中没有任何明显的错误。我知道 AWS 凭证很好,因为我可以在我的服务器控制台中检索所有 url,而不仅仅是我的 localhost 控制台。此外,我可以使用邮递员和 base64 编码毫无错误地上传照片。这些图像在服务器端应用程序的 webapp 前端中都显示没有问题。它专门检索有问题的 JBUILDER 的 url。
  • Cosole 和服务器环境完全不同。 Heroku 服务器可能没有初始化环境变量。您能否提及您将 aws 凭证保存在哪里。在yamlrb 文件中,或者您正在使用ENV
  • 我使用 ENV 保存变量 - 我知道它们在我的服务器上是正确的,因为在我的服务器控制台中,而不是 LOCALHOST 我可以上传图像并返回 URL。运行 heroku 的实时 webapp 能够毫无问题地返回 URL。只有在同一台服务器上尝试将这些 URL 呈现为 JSON 时,才会出现问题。

标签: ruby-on-rails ruby json paperclip jbuilder


【解决方案1】:

你可以试试这个来获取 json 响应中的 url,它适用于我把这个方法放在你的模型中。

def avatar_url
  avatar.url(:medium)
end

并从控制器调用此方法,通过覆盖 to_json()

render :json => @friends.to_json(:methods => [:avatar_url])

【讨论】:

    【解决方案2】:

    经过几个小时的研究,终于诊断出这个问题。

    问题在于“json.cache! @user do”——特别是.cache!——我想节省一些服务器周期并加快速度,所以这就是我最初实现的方式。

    无论如何,当我将 JBUILDER 代码更新为以下代码时,我不再收到 500 个服务器错误。

    我为我的用户工作的 Jbuilder 的片段

    json.id @user.id
    json.type @user.type
    json.f_name @user.f_name
    json.about @user.about
    json.email @user.email
    json.small_photo @user.profile_url_small
    json.medium_photo @user.profile_url_medium
    json.referral_code @user.referral_code
    json.education_level @user.education_level
    json.education_details @user.education_details.all
    json.all_subjects @user.subjects.all
    json.all_times @user.all_available_times.each do |time|
        json.day time.schedule.to_s
        json.time_block time.time_as_string
      json.next_occurrence time.schedule.next_occurrence(Time.now)
    end 
    

    【讨论】:

      【解决方案3】:

      在不知道错误的情况下很难说出问题可能是什么,但这些事情看起来很可疑或完全错误:

      json.avatar_url @user.avatar.url(:medium)
      json.photo_url @user.avatar.to_json
      

      第二个会给你额外的报价,你可能不想要。为什么两者都有?

      也在这里:

      json.all_times @user.all_available_times.each do |time|
        json.day time.schedule.to_s
        json.time_block time.time_as_string
        json.next_occurrence time.schedule.next_occurrence(Time.now)
      end 
      

      我想你想要这个:

      json.all_times @user.all_available_times do |time|
        json.day time.schedule.to_s
        json.time_block time.time_as_string
        json.next_occurrence time.schedule.next_occurrence(Time.now)
      end 
      

      【讨论】:

      • 感谢您的宝贵时间,但都没有帮助我解决我的具体问题。
      猜你喜欢
      • 2013-07-16
      • 2012-11-24
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 2014-02-18
      • 2016-04-29
      相关资源
      最近更新 更多