ruby-on-rails – 条件转换选项回形针

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 条件转换选项回形针前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
some research之后,我能够根据我的image_class列添加样式.

Model.rb

  1. has_attached_file :image,:styles => lambda { |attachment| attachment.instance.decide_styles }
  2.  
  3. def decide_styles
  4. styles = {}
  5.  
  6. case self.image_class
  7. when "poster"
  8. styles[:thumb] = ["30x45!",:jpg]
  9. styles[:standard] = ["185x278!",:jpg]
  10. styles[:expanded] = ["372x559!",:jpg]
  11. styles[:big] = ["600x900!",:jpg]
  12. when "cover"
  13. styles[:thumb] = ["30x45!",:jpg]
  14. styles[:standard] = ["300x1200!",:jpg]
  15. end
  16.  
  17. styles
  18. end

这很顺利,现在我也想添加条件convert_options.这种方式失败了.

  1. has_attached_file :image,:styles => lambda { |attachment| attachment.instance.decide_styles },:convert_options => lambda { |attachment| attachment.instance.decide_convert_options }
  2.  
  3. def decide_styles
  4. ...
  5. end
  6.  
  7. def decide_convert_options
  8.  
  9. opshunz = {}
  10. case self.image_class
  11. when "poster"
  12. opshunz[:thumb] = "-flop"
  13. opshunz[:standard] = "-flop"
  14. opshunz[:expanded] = "-flop"
  15. opshunz[:big] = "-flop"
  16.  
  17. when "cover"
  18. opshunz[:thumb] = "-enhance"
  19. opshunz[:standard] = "-enhance"
  20. end
  21.  
  22. opshunz
  23. end

错误

  1. NoMethodError: undefined method `instance' for :all:Symbol
  2.  
  3. from /Users/AnsPoluke/Sites/nulike/app/models/movie_image.rb:8:in `block in <class:MovieImage>'
  4. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:431:in `[]'
  5. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:431:in `process_options'
  6. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:423:in `extra_options_for'
  7. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:56:in `convert_options'
  8. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:79:in `block in processor_options'
  9. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:78:in `each'
  10. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:78:in `processor_options'
  11. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:462:in `block in post_process_style'
  12. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:461:in `each'
  13. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:461:in `inject'
  14. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:461:in `post_process_style'
  15. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:454:in `block in post_process_styles'
  16. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:453:in `each'
  17. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:453:in `post_process_styles'
  18. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:445:in `block (2 levels) in post_process'
  19. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:393:in `_run__3861360263242897910__image_post_process__callbacks'
  20. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
  21. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/callbacks.rb:36:in `run_paperclip_callbacks'
  22. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:443:in `block in post_process'
  23. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:383:in `_run__3861360263242897910__post_process__callbacks'
  24. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
  25. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/callbacks.rb:36:in `run_paperclip_callbacks'
  26. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:442:in `post_process'
  27. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:114:in `assign'
  28. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/has_attached_file.rb:66:in `block in define_setter'
  29. from (irb):2
  30. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
  31. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
  32. from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
  33. from bin/rails:4:in `require'

任何想法为什么它与样式完美配合但转换失败?

解决方法

注意:尚未验证工作代码

似乎传入块的参数:convert_options已经是一个实例,而不是附件(而不是样式选项,它是一个附件)

尝试:

  1. convert_options: lambda { |instance| instance.decide_convert_options }

如果你提取配置数据,你的代码会更好看,例如:

  1. has_attached_file :image,styles: lambda { |attachment| attachment.instance.image_options[:styles] },convert_options: lambda { |instance| instance.image_options[:convert_options] }
  2.  
  3.  
  4. IMAGE_OPTIONS = {
  5. poster: {
  6. styles: {
  7. thumb: ["30x45!",:jpg],standard: ["185x278!",expanded: ["372x559!",:jpg]
  8. big: ["600x900!",:jpg]
  9. },convert_options: {
  10. thumb: "-flop",standard: "-flop",expanded: "-flop",big: = "-flop"
  11. }
  12. },cover: {
  13. styles: {
  14. thumb: ["30x45!",standard: ["300x1200!",convert_options: {
  15. thumb: "-enhance",standard: "-enhance"
  16. }
  17. }
  18. }
  19.  
  20. def image_options
  21. IMAGE_OPTIONS[self.image_class]
  22. end

我希望有所帮助

更新:

看起来你的convert_options没有在这里设置:
https://github.com/thoughtbot/paperclip/blob/a93dfc773b4fd649db4d1281b42a2a71b1ae72ff/lib/paperclip/style.rb#L55

看起来他们建议传递带有样式的convert_options,如本规范:https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41

你能试试吗?所以完全删除convert_options,并在您的配置中返回哈希,如:

  1. IMAGE_OPTIONS = {
  2. poster: {
  3. styles: {
  4. thumb: {
  5. geometry: "30x45!",format: :jpg,convert_options: '-flop',},standard: {...}
  6. expanded: {...}
  7. big: {...}
  8. }
  9. },cover: {
  10. styles: {...}

猜你在找的Ruby相关文章