在我的Rails应用程序中,我有一些默认的gravatar图像,但我需要它在两页中的大小不同。 比如,在第一页图像高度应该是50px,在2nb页面上 - 100px。
问题是:我应该如何设置我的代码以避免重复方法并实现我的目标? 附:我相信我应该在“gravatar_show”中为“image tag”添加一些更改,但我的所有尝试都失败了。
def gravatar_for(user) # it's for 1'st page
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag(gravatar_url, alt: user.name, class: "gravatar img-rounded")
end
def gravatar_show(user) # it's for 2'nd page(show)
gravatar_for user
........# missing code......
end
======================================
当然,我可以复制并更改每个类,并在css中使用不同的类但它看起来很难看
def gravatar_for(user)
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag(gravatar_url, alt: user.name, class: "gravatar img-rounded")
end
def gravatar_show(user)
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag(gravatar_url, alt: user.name, class: "gravatar-show img-rounded")
end
答案 0 :(得分:0)
使用延迟String
评估"#{variable}"
:
def gravatar_for(user, show = nil)
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
mod = "-show" if show
image_tag(gravatar_url, alt: user.name, class: "gravatar#{mod} img-rounded")
end