7

问题

我有一个运行三元运算的辅助方法。结果操作数都是 image_tag 方法。当我从视图中调用这个助手时,":size" 选项不会转换为 html 中的高度和宽度标签。当我将任一 image_tag 方法直接复制并粘贴到我的视图文件中时,一切正常。

application_helper.rb

def display_portrait(candidate)
  if candidate.portrait_url
    image_tag(candidate.portrait_url.to_s, :size => "10x10")
  else
    image_tag("missing_portrait.png", :size => "10x10")
  end
end

视图.html.erb

<%= display_portrait candidate %> DOES NOT GENERATE HEIGHT AND WITH ATTR'S

<%= image_tag("missing_portrait.png", :size => "10x10") %> DOES GENERATE ATTR'S

助手仍然生成具有正确来源的锚点;它只缺少高度和宽度属性。

感谢您花点时间提供帮助,

迈克尔

4

3 回答 3

0

如果我是你,我无论如何都不会使用“大小”,除非你不关心 IE<9。我宁愿使用样式属性。

PS:特别是对于这个问题,我会尝试使用 width: 和 height: 来看看这是否会改变任何东西。我知道 Rails 中的一些助手有不同的行为,具体取决于它们被调用的位置。

于 2014-02-11T22:07:56.537 回答
0

首先通过在显示肖像方法中写入 puts 语句来检查条件,然后检查是什么

进来这个方法的参数。

您也可以在视图中执行此操作

   <%= image_tag(@image.portrait.url(:thumb):alt => "Description") %>

希望对你有帮助......

于 2012-05-08T15:25:13.340 回答
0

application_helper.rb

def display_portrait(candidate)
  if candidate == 1
    image_tag(candidate, :size => "10x10")
  else
    image_tag("missing_portrait.png", :size => "10x10")
  end
end

新的.html.erb

在这种观点下,

<%= display_portrait 1 %>

生成,

<img alt="1" height="10" src="/assets/1" width="10" />

<%= display_portrait 0 %>

生成,

<img alt="Missing_portrait" height="10" src="/assets/missing_portrait.png" width="10" />

你的 If 条件一定有问题。

于 2012-05-08T16:29:31.907 回答