【问题标题】:Rails: Active Admin Association Image columnRails:活动管理员关联图像列
【发布时间】:2013-05-19 23:04:36
【问题描述】:

我是 ruby​​ on rails 的新手,刚刚安装了活动管理员并尝试自定义

观看次数。

我有一个产品和图片表。每张图片都属于一个产品。

现在我想在显示产品页面时显示一个带有相关图像的列。

目前只有 image_url 文本不起作用。以后我想做

让图片以 50x50px 显示。

我该怎么做? (图像模型:名称:字符串 image_url:文本)

这是我所做的:

ActiveAdmin.register Product do

index do
    column "Image" do |image|
        image.image_url
    end
    column :name
    column :preview_text
    column :full_text
    column :price, :sortable => :price do |product|
      div :class => "price" do
          number_to_currency product.price
     end
    end
 default_actions
 end
end

我不知道如何用“做图像”来修复那个部分。我是 Rails 的初学者 2 天 exp..

似乎语法错误并抛出错误:

undefined method `image_url' for #<Product:0x00000101b5a458>

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 activeadmin


    【解决方案1】:

    你需要从图片对象product.image.image_url中获取image_url

    关于图片大小,你可以用你想要的大小来显示图片,像这样

    column "Image" do |product|
      image_tag product.image.image_url, class: 'my_image_size'
    end
    

    .css

    .my_image_size {
      width: 50px;
      height: 50px;
    }
    

    或者您可以使用诸如CarrierWave 之类的 gem 来调整图像本身的大小。

    【讨论】:

    • 或者你可以直接做image_tag product.image.image_url, size: "50x50"而不需要处理css。
    【解决方案2】:
    column "Image" do |product|
      image_tag (product.image,width:100,height:80) 
    end
    
    
    references
    image=it's belongs to your database column name
    product=it creates new object for your image by activeadmin register class method.
    

    【讨论】:

    • 你应该添加一些解释。它总是受到赞赏... ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2012-02-27
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多