Rails Carrierwave Imagemagik - 指定大小

时间:2015-10-09 06:36:31

标签: ruby-on-rails carrierwave minimagick

我正在尝试使用Rails 4制作应用。我使用了带有'mini_magick'的载波。处理图像。

我创建了一个名为:avatar_uploader.rb的文件,其中包含以下内容:

  process :resize_to_fit => [800, 800]
  # Create different versions of your uploaded files:
  version :thumb do
      process :resize_to_fill => [200,200]
    end

    version :profile do
        process :resize_to_fill => [345,245]
    end


    version :wider do
      process :resize_to_fill => [951,245]
    end

    version :small do
          process :resize_to_fill => [35,35]
    end

名为:small的版本最初称为:logo。但是,我想要使用它的一些模型有名为:logo的属性,所以当我想使用该版本时,我在该行中有.logo.logo。

然而,我发布了这个问题并且答案(在标题Trivia - 第2点下)建议不要连续两次使用相同的单词。 http://stackoverflow.com/questions/32916133/rails-how-to-show-attribute-of-an-associated-model

这就是为什么我将avatar_uploader.rb中的版本名称更改为小。

但是,现在当我尝试渲染显示页面时:as

<%= link_to university_path(@profile.university) do %>

        <%= image_tag(@profile.university.try(:logo).small) %>

        <%= @profile.university.name %>

我收到此错误:

NoMethodError at /profiles/3
undefined method `small' for nil:NilClass

名称显示正确,图像显示为带有问号的框。我在代码的其他部分使用相同的图像,具有不同的版本大小,并且它可以正常工作。

是否需要做一些事情来刷新头像上传者的内容?

1 个答案:

答案 0 :(得分:0)

在您的university.rb文件中,您应该有一行mount_uploader :logo, AvatarUploader

你应该有一个名为logo的属性,它是你模型中的一个字符串。

我假设这个模型叫做大学。然后在您的图片标记中,您需要做的只是image_tag @profile.university.logo.small.url