Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5592813
  • 博文数量: 745
  • 博客积分: 10075
  • 博客等级: 上将
  • 技术积分: 7716
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-29 12:09
文章分类

全部博文(745)

文章存档

2019年(1)

2016年(1)

2010年(31)

2009年(88)

2008年(129)

2007年(155)

2006年(197)

2005年(143)

分类:

2007-12-26 00:31:41

在《Pragmatic Bookshelf - Agile Web Development with Rails 2nd Edition2007》136页出现的代码:
<%= hidden_div_if(@cart.items.empty?, :id => "cart" ) %>
<%= render(:partial => "cart" , :object => @cart) %>
以及store_helper.rb中代码:
module StoreHelper
def hidden_div_if(condition, attributes = {})
if condition
attributes["style" ] = "display: none"
end
attrs = tag_options(attributes.stringify_keys)
"
"
end
end
按照这样的方式,实际显示效果相当差,在网上找到一段比较好的修改代码:
<% hidden_div_if(@cart.items.empty?, :id => "cart") do %>
    <%= render(:partial => "cart", :object => @cart) %>
<% end %>
以及store_helper.rb:
module StoreHelper
def hidden_div_if(condition, attributes = {}, &block)
    if condition
      attributes["style"] = "display: none"
    end

    attrs = tag_options(attributes.stringify_keys)

    content = capture(&block)

    concat("
", block.binding)
    concat(content, block.binding)
    concat("
", block.binding)
end
end
按照这样的方式对代码进行重构,丝毫不影响页面的显示效果,看来尽信书不如无书真是有道理啊。
阅读(2113) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~