开发者

rails haml ugly weird behaviour

开发者 https://www.devze.com 2023-02-17 19:01 出处:网络
I have the following helper: def feeder value=true if feeder? == value haml_tag :span, :<, :class => \'selected\' do

I have the following helper:

def feeder value=true
  if feeder? == value
    haml_tag :span, :<, :class => 'selected' do
      yield
    end
  else
    yield
  end
end

And the following in a view:

- feeder(false) do
  = link_to 'Leda', :root

This works as expected in haml non-ugly mode (development environment == haml indented code). However in production mode (e.g. haml ugly rendering) i get this html:

<h1> 
<a href="/leda/">Leda</a> 
<!-- THIS SHOULD NOT BE HERE >>> -->
<!DOCTYPE html> 
<html> 
<开发者_开发技巧head> 
<meta charset='utf-8'> 
<title>Feeder</title> 
<meta name="csrf-param" content="authenticity_token"/> 
<meta name="csrf-token" content="8kp4xt6ZJU2nL5uLgVBW6BcB/RTA75QwynKvZTMtNF8="/> 
<link href="/leda/stylesheets/jquery-ui/smoothness/jquery-ui.css?1297978005" media="screen" rel="stylesheet" type="text/css" /> 
<link href="/leda/stylesheets/admin.css?1298951622" media="screen" rel="stylesheet" type="text/css" /> 
<script src="/leda/javascripts/jquery.js?1297978005" type="text/javascript"></script> 
<script src="/leda/javascripts/jquery-ui.js?1297978004" type="text/javascript"></script> 
<script src="/leda/javascripts/rails.js?1297978005" type="text/javascript"></script> 
<script src="/leda/javascripts/application.js?1300153136" type="text/javascript"></script> 

</head> 
<body> 
<div id='wrapper'> 
<div id='header'> 
<h1> 
<a href="/leda/">Leda</a> 
<!-- <<< END -->
 &raquo; 
<span class='selected'><a href="/leda/servers/GVM-S1">gvm1</a></span> 
</h1>

Replacing the else clause with this:

else
  haml_tag :span do
    yield
  end
end

fixes the problem, but i can't possibly understand why/how. What puzzles more is that it only fails on haml ugly rendering mode.


Why not just do it all in the view, like this?

%span{:class => blah.feeder? && "selected"}= link_to 'Blah', blah

(haml docs on this are right here.)

Your helper version seems unnecessarily complex.

0

精彩评论

暂无评论...
验证码 换一张
取 消