How can i add a url to my cover_image on rails 4 using carrierware

时间:2022-02-08 07:26:16

To be specific, Let's say i have model called Products and it has a column called :cover_image which holds the picture

具体来说,假设我有一个名为Products的模型,它有一个名为:cover_image的列,用于保存图片

I have mounted the uploader and everything works fine

我安装了上传器,一切正常

Except that for now i want to add a custom url to a new product or existing one

除了现在我想要为新产品或现有产品添加自定义网址

for example

I want to do like this

我想这样做

p = Product.last
p.cover_image = "something"

it didn't work

它不起作用

I added

attr_accessor :cover_image_url

and now i'm able to do

现在我能够做到

p.cover_image_url = "something"

once i do

我曾经做过

p.save

it acts like everything went fine

它就像一切都很好

except the cover_image column still empty

除了cover_image列仍然为空

how can i populate it with a link manually ?

如何手动填充链接?

Thanks in advance

提前致谢

1 个解决方案

#1


0  

If you need a custom url for some Products and others you are happy with the uploaded image url, here's what I would do. (I recommend leaving the cover_image_url as it is and create a separate column for custom url)

如果您需要某些产品和其他产品的自定义网址,您对上传的图片网址感到满意,这就是我要做的。 (我建议保持原样保留cover_image_url并为自定义网址创建单独的列)

1 - Create a separate column called custom_url (leave the cover_image_url)

1 - 创建一个名为custom_url的单独列(保留cover_image_url)

2 - remove attr_accessor :cover_image_url and leave it as it was (because it overrides the up loader functionality)

2 - 删除attr_accessor:cover_image_url并保持原样(因为它会覆盖向上加载器功能)

3 - Update the product model to have a method to return url

3 - 更新产品型号以获得返回URL的方法

class Product < ActiveRecord::Base
  # ... other code   

  def image_url
    # return custom_url if it has one, or the standard url
    # you can change this logic as you want
    custom_url || cover_image_url
  end
end

4 - Now, instead of calling p.cover_image_url I'll call p.image_url

4 - 现在,我将调用p.image_url而不是调用p.cover_image_url

p = Product.last
p.image_url

#1


0  

If you need a custom url for some Products and others you are happy with the uploaded image url, here's what I would do. (I recommend leaving the cover_image_url as it is and create a separate column for custom url)

如果您需要某些产品和其他产品的自定义网址,您对上传的图片网址感到满意,这就是我要做的。 (我建议保持原样保留cover_image_url并为自定义网址创建单独的列)

1 - Create a separate column called custom_url (leave the cover_image_url)

1 - 创建一个名为custom_url的单独列(保留cover_image_url)

2 - remove attr_accessor :cover_image_url and leave it as it was (because it overrides the up loader functionality)

2 - 删除attr_accessor:cover_image_url并保持原样(因为它会覆盖向上加载器功能)

3 - Update the product model to have a method to return url

3 - 更新产品型号以获得返回URL的方法

class Product < ActiveRecord::Base
  # ... other code   

  def image_url
    # return custom_url if it has one, or the standard url
    # you can change this logic as you want
    custom_url || cover_image_url
  end
end

4 - Now, instead of calling p.cover_image_url I'll call p.image_url

4 - 现在,我将调用p.image_url而不是调用p.cover_image_url

p = Product.last
p.image_url