在《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
按照这样的方式对代码进行重构,丝毫不影响页面的显示效果,看来尽信书不如无书真是有道理啊。
阅读(2154) | 评论(0) | 转发(0) |