使用rails和paperclip

时间:2016-03-11 12:22:10

标签: ruby-on-rails ruby paperclip

我在我的rails应用程序中安装了paperclip,一切正常,但问题是我为我的Customer实体添加了两个图像,当我在前端(通过API调用)返回一个Customer时,我只得到1个url 。 这是代码:

客户实体的控制台日志:

Object {id: 2,
name: "carlos",
surname: "ruiz",
email: "carlos@car.com",
password_digest:"$2a$10$pJnjlXk5YUq0YrD5WhtmB.nDL3oeKo2U6S9JKSjhbfWf5ti6Ex6k2"…}
created_at: "2016-02-10T18:52:11.000Z"
email: "carlos@car.com"
header_path_content_type: "image/png"
header_path_file_name: "clients.png"
header_path_file_size: 1445943
header_path_updated_at: "2016-03-11T11:49:58.000Z"
image_path: "/system/customers/image_paths/000/000/002/original/earnings.png?1457696996"
image_path_content_type: "image/png"
image_path_file_name: "earnings.png"
image_path_file_size: 1587885
image_path_updated_at: "2016-03-11T11:49:56.000Z"

正如您所看到的,我有image_path路径:/ system / customers / ...但我没有header_path路径,只有内容,文件大小,名称和日期。

以下是模型:

  class Customer < ActiveRecord::Base
    ...
    has_attached_file :image_path, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/user_images/missing.png"
    has_attached_file :header_path, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/user_headers/missing.png"
    validates_attachment_content_type :image_path, content_type: /\Aimage\/.*\Z/
    validates_attachment_content_type :header_path, content_type: /\Aimage\/.*\Z/
  end

迁移:

class AddUserPhoto < ActiveRecord::Migration

  def up
    add_attachment :customers, :image_path
    add_attachment :customers, :header_path
  end

  def down
    remove_attachment :customers, :image_path
    remove_attachment :customers, :header_path
  end
end

奇怪的是,在控制台和后端管理部分,我可以上传并查看两个图像,如果我在控制台中键入Customer.first.header_path,我可以看到正确的路径。但是当我通过api发送客户时,我遇到了这个问题。 以下是返回客户的功能:

  def client_details
    hairdresser_id =  params[:api][:hairdresser_id]
    token =           params[:api][:token]
    customer_id =     params[:api][:customer_id]

    if token.nil? or token.empty?
      render :json => {:error => "not authorized"}
    end

    if allowed_customers_id.include? customer_id.to_i
      customer = Customer.find customer_id

      render :json => {
        :customer_details => customer,
        :customer_addresses => customer.addresses,
        :customer_note => note,
        :customer_appointments => Appointment.where(:customer_id => customer.id).reverse,
        :appointment_services => Appointment.where(:customer_id => customer.id).where.not(:status => 'cancelled').map{|app| app.service_appointments.first.service.name}.reverse,
        :error => ''
      }
    else
      render :json => {:error => 'Not authorized'}
    end
  end

你知道我错过了什么吗?

0 个答案:

没有答案