Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-曲别针-设置随机更改默认图像_Ruby On Rails_Ruby_Ruby On Rails 3_Image_Paperclip - Fatal编程技术网

Ruby on rails Rails-曲别针-设置随机更改默认图像

Ruby on rails Rails-曲别针-设置随机更改默认图像,ruby-on-rails,ruby,ruby-on-rails-3,image,paperclip,Ruby On Rails,Ruby,Ruby On Rails 3,Image,Paperclip,我在Rails 3.2应用程序中使用回形针上传图像。 目标是在用户上传自己的图片之前,每次创建新帖子时向用户显示一个不同的(随机变化的)默认图片 我在这里发现了许多问题,但不幸的是,这些解决方案对我不起作用 这是post.rb模型: has_attached_file :image, styles: { medium: "320x240>"} validates_attachment :image, content_type: { content_type: ['image/jpeg',

我在Rails 3.2应用程序中使用回形针上传图像。 目标是在用户上传自己的图片之前,每次创建新帖子时向用户显示一个不同的(随机变化的)默认图片

我在这里发现了许多问题,但不幸的是,这些解决方案对我不起作用

这是post.rb模型:

has_attached_file :image, styles: { medium: "320x240>"}
validates_attachment :image, 
content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png'] }, 
size: { less_than: 2.megabytes }

belongs_to :user
has_attached_file :image, styles: { thumb: "100x100", small: "160x120", medium: "320x240>", large: "640x480", fullscreen: "1000x300#" },
:default_url => '/assets/1.jpg'
我不想使用
:default_url=>'/assets/1.jpg'
我想设置一些东西,随机显示十幅图像中的一幅(1.jpg到10.jpg)

如果我尝试使用
:default_url=>lambda{'/assets/#{rand(5)}.jpg'}
我会得到以下错误:
帖子中的ArgumentError#new-参数数量错误(1代表0)

问题是,要想做你想做的事,回形针必须在默认url中支持lambdas。
考虑到我认为它不正确(但我不确定,我很久以前读过一些关于它的文章),我建议你采取“简单路线”,即使感觉不太正确(但不太正确)。使用视图和辅助对象,执行以下操作:

post_helper.rb

module PostHelper

  def post_image_url(post)
    return post.image.url if post.image.exists?

    number_between_0_and_3 = rand(4)

    "my_random_image_#{ number_between_0_and_3 }.jpg"
  end

end
<%= image_tag(post_image_url(@post), alt: 'Dunno') %>
module PostHelper

  def post_image_url(post)
    return post.image.url if post.image.exists?

    # Here is the change, you must ensure post.user is always present anyway!
    number_between_0_and_3 = post.user.id % 4

    "my_random_image_#{ number_between_0_and_3 }.jpg"
  end

end
my_view.html.erb

module PostHelper

  def post_image_url(post)
    return post.image.url if post.image.exists?

    number_between_0_and_3 = rand(4)

    "my_random_image_#{ number_between_0_and_3 }.jpg"
  end

end
<%= image_tag(post_image_url(@post), alt: 'Dunno') %>
module PostHelper

  def post_image_url(post)
    return post.image.url if post.image.exists?

    # Here is the change, you must ensure post.user is always present anyway!
    number_between_0_and_3 = post.user.id % 4

    "my_random_image_#{ number_between_0_and_3 }.jpg"
  end

end
这将根据id授予用户一个介于my_random_image_0.jpg和my_random_image_3.jpg之间的随机图像,因此它不会跨页面更改,但仍然是伪随机的


此外,如果用户检查其个人资料,则头像图像将显示为空,而不是显示在那里(因此用户知道他必须添加头像,以获得更好的用户体验)。

非常感谢您的快速回复!您提出了一个“view.html.erb”的建议,但是我应该在post.rb模型中而不是在:default_url=>'/assets/1.jpg'中编写什么呢?我可以删除它吗?@YvonC:两者都可以,我建议你总是有一个默认的url,所以保留它,即使你根本不使用它。也请看我的编辑,有一些改进谢谢@Fire Dragon DoL;这个解决方案是完美的。对于其他人,如果您使用不同的图像大小,只需向助手方法添加另一个参数:image_标记(post_image_url(post,'medium'))。