使用 JBUILDER 渲染 Paperclip URL
Rendering Paperclip URL's using JBUILDER
我正在 Rails 上用 Ruby 构建一个 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 的某些东西不能混用,我似乎无法深入了解它。任何帮助是极大的赞赏。
在不知道错误的情况下很难说出问题可能是什么,但这些事情看起来很可疑或只是错误的:
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
经过几个小时的研究,终于诊断出这个问题。
问题出在“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
您可以试试这个在 json 响应中获得 url,它对我在您的模型中放置此方法有效。
def avatar_url
avatar.url(:medium)
end
并通过重写 to_json()
从控制器调用此方法
render :json => @friends.to_json(:methods => [:avatar_url])
我正在 Rails 上用 Ruby 构建一个 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 的某些东西不能混用,我似乎无法深入了解它。任何帮助是极大的赞赏。
在不知道错误的情况下很难说出问题可能是什么,但这些事情看起来很可疑或只是错误的:
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
经过几个小时的研究,终于诊断出这个问题。
问题出在“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
您可以试试这个在 json 响应中获得 url,它对我在您的模型中放置此方法有效。
def avatar_url
avatar.url(:medium)
end
并通过重写 to_json()
从控制器调用此方法render :json => @friends.to_json(:methods => [:avatar_url])