size:attribute在这段代码中做了什么?

时间:2012-05-04 10:06:11

标签: ruby-on-rails-3

module UsersHelper

  # Returns the Gravatar (http://gravatar.com/) for the given user.
  def gravatar_for(user, options = { size: 10 })
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    size = options[:size]
    gravatar_url = "http://gravatar.com/avatar/#{gravatar_id}.png?s=#{size}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end
end

我已经使用过此代码并假设它会改变gravatar的大小,但它似乎对它没有影响?我错过了什么吗?我还尝试将视图中的值更改为:

<%= gravatar_for @user, size: 10 %>
<%= @user.name %>

要查看这是否有所改变,无济于事。

1 个答案:

答案 0 :(得分:1)

假设你的帮助模块如下

module UsersHelper

   # Returns the Gravatar (http://gravatar.com/) for the given user.
  def gravatar_for(user, options = { size: 50 })

    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)

    size = options[:size]

    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"

    image_tag(gravatar_url, alt: user.name, class: "gravatar")

  end

end

然后在视图中传递您的尺寸

<%= gravatar_for @user, :size => 30 %>