有条件地将样式应用于 Rails 3.1 中的回形针附件

2023-11-26

我有以下型号:

Post.rb

  class Post < ActiveRecord::Base

  belongs_to :category
  has_many :attachments, :dependent => :destroy
  has_many :citations, :dependent => :destroy

  validates :title, :category_id, :content, :presence =>true

  acts_as_taggable_on :keywords
  accepts_nested_attributes_for :attachments, :allow_destroy => true, 
         :reject_if => proc { |attributes| attributes['photo'].blank?}
  accepts_nested_attributes_for :citations, :allow_destroy=>true

end

附件.rb

  class Attachment < ActiveRecord::Base

  belongs_to :post

  has_attached_file :photo, :styles => { :medium => "637x471>", 
                :thumb => Proc.new { |instance| instance.resize },
                :carousel => Proc.new { |instance| instance.decide_style }
                },
                :url => "/pictures/:style/:basename.:extension",
                :path =>":rails_root/public/pictures/:style/:basename.:extension"


  validates_attachment_content_type :photo, :content_type => ['image/png', 'image/jpg', 'image/jpeg']                  
    validates_attachment_size :photo, :less_than => 2.megabytes         


   ### End Paperclip ####
  def decide_style

   # catid = Post.find(param[:post_category_id])
   # something here to get the selection from the form.

      if(catid == 2)
      # "400x800>" or custom style here
      end

      end


 def resize     
 geo = Paperclip::Geometry.from_file(photo.to_file(:original))

 ratio = geo.width/geo.height  

 min_width  = 142
 min_height = 119

 if ratio > 1
   # Horizontal Image
   final_height = min_height
   final_width  = final_height * ratio
   "#{final_width.round}x#{final_height.round}!"
 else
   # Vertical Image
   final_width  = min_width
   final_height = final_width * ratio
   "#{final_height.round}x#{final_width.round}!"
  end
 end 

 end

我正在尝试根据表单下拉列表中的选择有条件地应用样式。我只是不确定应该选择哪种风格的决定是在哪里实施的。

想法?

Rishi


看来您正在尝试将其基于category_id值于Post型号,是吗?如果是这样,您实际上可以传递 lambda 作为:styles附件选项中的条目,其中包含附件作为属性。

has_attached_file :photo,
    :styles => lambda { |attachment| {
      :medium => "637x471>", 
      :thumb => attachment.instance.resize,
      :carousel => attachment.instance.decide_style,
    } },
    :url => "/pictures/:style/:basename.:extension",
    :path =>":rails_root/public/pictures/:style/:basename.:extension"

The attachment is a Paperclip::Attachment对象,并调用instance其上的方法返回您的模型实例。

然后在你的decide_style方法,您可以根据您的模型category_id根据需要值。

def decide_style
  case category_id
  when 1 then "200x400>"
  when 2 then "400x800>"
  else "50x50#"
  end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有条件地将样式应用于 Rails 3.1 中的回形针附件 的相关文章

随机推荐

  • 托管和设置自己的闪亮应用程序,无需闪亮服务器

    我正在尝试为我的同事提供闪亮的应用程序 而他们无需运行甚至无需安装 R 所以我读了这个网页并找到了这句话 如果您熟悉网络托管或有权访问 IT 部门 您可以自己托管您的 Shiny 应用程序 在下面 分享为网页 部分 我怎样才能做到这一点 问
  • Try It 演示中的 Google Drive SDK 更新(删除/插入)所有者权限 500 错误

    我正在开发一个应用程序 它应该为 Google Apps 管理员提供重新分配文件所有权的能力 目前我正在使用 Google Drive SDK for NET 和 C 代码 当我在 Google Drive SDK 测试演示中进行身份验证时
  • 如何在 Twig 中使用 sprintf 格式化数字?

    我需要格式化一个数字 在 PHP 中 我会使用sprintf 05d lineNumber 在 Symfony 2 Twig 模板中 我应该使用什么代码 If i之前是 4 然后将其添加到 Twig 模板后 02d format i 它将成
  • 找出 ASP.NET 身份验证票证过期的原因

    我需要帮助弄清楚为什么我的身份验证票证在我的托管网站上大约一个小时后就过期了 但是 如果我在本地运行该站点 身份验证票证将在正确的时间内保持活动状态 这是我的本地主机 web config
  • Steam 市场货币和 XML 格式

    我试图在市场上以某种货币获取商品页面 尝试添加 Accept Language ru RU r n and Accept Language ru RU ru r n and Accept Language ru ru RU q 0 8 r
  • git 清理旧分支

    我想创建一个 git 命令来删除当前分支中包含所有提交的任何分支 例如 git branch groups master git cleanup branches deleted groups all commits are include
  • 删除向量开头和结尾的零

    我有一个像这样的向量 x lt c 0 0 0 0 4 5 0 0 3 2 7 0 0 0 我只想保留位置 5 到 11 的元素 我想删除开头和结尾的零 对于这个向量来说 这很容易 因为它很小 我有非常大的数据 需要所有向量的通用信息 尝试
  • PostgreSQL 忽略时间戳列上的索引

    我创建了下表和索引 CREATE TABLE cdc auth user cdc auth user id bigint NOT NULL DEFAULT nextval cdc auth user id seq regclass cdc
  • 使用键盘快捷键打开浏览器操作的弹出窗口

    我正在开发一个带有浏览器操作弹出窗口的 Google Chrome 扩展程序 当用户单击该图标时 会出现弹出窗口 Is there a way to open this popup with a keyboard shortcut like
  • 如何检查 zip 存档中是否存在文件

    我有 zip 存档 解压后我需要检查 zip 存档中是否存在 moduleConfig xml 我怎样才能做到这一点 我试试这个 zip new ZipArchive if zip gt open test zip TRUE if file
  • 获取 SQL Server 2000 中最后插入的 UNIQUEIDENTIFIER

    The OUTPUT子句与 SQL Server 2005 兼容 但与 SQL Server 2000 不兼容 如何将此命令转换为在 SQL Server 2000 中工作 CREATE TABLE sample ID uniqueiden
  • 线程接收错误参数

    我需要在线程中运行具有给定参数的方法 我注意到当我运行它时 参数错误 对于给出的示例 我有一个数组int output与数字1 7 对于每个数字 我使用以下方法创建一个线程WriteInt i 我希望输出以任意顺序为 1 7 但我始终看到一
  • AWK 输出到 bash 数组

    我试图将一个简单命令的内容放入 bash 数组中 但是遇到了一些麻烦 df h awk print 5 6 给出我的系统上文件系统的使用百分比 输出看起来像这样 1 dev 1 dev shm 1 var run 0 var lock 22
  • ConcurrentDictionary 和 ConcurrentQueue 的这种组合是线程安全的吗?

    我在以下代码中使用 NET 4 中的 ConcurrentDictionary 和 ConcurrentQueue 类 这段代码是线程安全的吗 如果不是 我怎样才能使其线程安全 public class Page public string
  • 如何从 PyQt 中的 QListView 中选择项目

    我是 PyQt 的新手 所以我试图从 QListView 中获取所选项目 我能够获取所选项目的索引 但我无法获取索引的值 请有人帮助我 这是代码 import sys import os from PyQt4 import QtCore Q
  • 通过 COM 从 VC++ 实例化 C# 对象时出现类未注册错误

    在 VC 项目中 我尝试创建 C 项目中包含的 C 类的实例 通过 COM Facts C 和 C 项目均使用 NET 4 0 编译 C dll 正在使用注册regasm 代码库 CSharpProjectName dll 并且 Windo
  • 在canEditRowAtIndexPath方法中,UITableView的reloadData无法正常工作?

    在我的应用程序中 我重新加载 TableView tablView reloadData 从 TableView 中删除行后canEditRowAtIndexPath方法总是调用 透水 总行数 例如 如果我有5我的 TableView 上的
  • Laravel 5 实现多个 Auth 驱动程序

    Synopsis 我正在构建一个至少具有两个身份验证级别的系统 并且两个级别在数据库中都有单独的用户模型和表 在谷歌上快速搜索 迄今为止唯一的解决方案是使用 MultiAuth 包 该包可以在多个驱动程序上安装Auth My goal 我正
  • 可能(x) 和 __builtin_expect((x),1)

    我知道内核使用likely and unlikely宏惊人 宏的文档位于内置函数 long builtin expect long exp long c 但他们并没有真正讨论细节 编译器到底是如何处理的likely x and builti
  • 有条件地将样式应用于 Rails 3.1 中的回形针附件

    我有以下型号 Post rb class Post lt ActiveRecord Base belongs to category has many attachments dependent gt destroy has many ci