ruby on rails - How to get if or unless statement to work over multiple lines in erb template -


i'm trying write in logic erb template in chef cookbook. have following, thought work. @ moment attribute there nil, it's not skipping whole block thought would. how top statement cause template reader skip whole block?

    <% unless node['base']['logstash-forwarder']['nginx'].nil? %>       <%= "{" %>       <%= "\"paths\": [" %>       <% node['base']['logstash-forwarder']['nginx'].each |path| %>         <% unless path.equal? node['base']['logstash-forwarder']['nginx'].last %>         <%= "\"#{path}\"," %>         <% end %>       <% end %>     <%= "\"#{node['base']['logstash-forwarder']['nginx'].last}\"" %>     <%= " ]," %>     <%= "\"fields\": { \"type\": \"nginx-access\" }" %>     <%= "}" %>   <% end %> 

your exact code above works precisely expected in erb, see:

[122] pry(main)> e = erb.new <<'eoi' [122] pry(main)*  <% unless node['base']['logstash-forwarder']['nginx'].nil? %> [122] pry(main)*       <%= "{" %> [122] pry(main)*       <%= "\"paths\": [" %> [122] pry(main)*       <% node['base']['logstash-forwarder']['nginx'].each |path| %> [122] pry(main)*         <% unless path.equal? node['base']['logstash-forwarder']['nginx'].last %> [122] pry(main)*         <%= "\"#{path}\"," %> [122] pry(main)*         <% end %> [122] pry(main)*       <% end %> [122] pry(main)*     <%= "\"#{node['base']['logstash-forwarder']['nginx'].last}\"" %> [122] pry(main)*     <%= " ]," %> [122] pry(main)*     <%= "\"fields\": { \"type\": \"nginx-access\" }" %> [122] pry(main)*     <%= "}" %> [122] pry(main)*   <% end %> [122] pry(main)* eoi => #<erb:0x007fe74bb35ff8  @encoding=#<encoding:utf-8>,  @filename=nil,  @lineno=0,  @safe_level=nil,  @src=   "#coding:utf-8\n_erbout = ''; _erbout.concat \" \";  unless node['base']['logstash-forwarder']['nginx'].nil? ; _erbout.concat \"\\n      \"\n; _erbout.concat(( \"{\" ).to_s); _erbout.concat \"\\n      \"\n; _erbout.concat(( \"\\\"paths\\\": [\" ).to_s); _erbout.concat \"\\n      \"\n;  node['base']['logstash-forwarder']['nginx'].each |path| ; _erbout.concat \"\\n        \"\n;  unless path.equal? node['base']['logstash-forwarder']['nginx'].last ; _erbout.concat \"\\n        \"\n; _erbout.concat(( \"\\\"\#{path}\\\",\" ).to_s); _erbout.concat \"\\n        \"\n;  end ; _erbout.concat \"\\n      \"\n;  end ; _erbout.concat \"\\n    \"\n; _erbout.concat(( \"\\\"\#{node['base']['logstash-forwarder']['nginx'].last}\\\"\" ).to_s); _erbout.concat \"\\n    \"\n; _erbout.concat(( \" ],\" ).to_s); _erbout.concat \"\\n    \"\n; _erbout.concat(( \"\\\"fields\\\": { \\\"type\\\": \\\"nginx-access\\\" }\" ).to_s); _erbout.concat \"\\n    \"\n; _erbout.concat(( \"}\" ).to_s); _erbout.concat \"\\n  \"\n;  end ; _erbout.concat \"\\n\"\n; _erbout.force_encoding(__encoding__)"> [123] pry(main)> node = { 'base' => { 'logstash-forwarder' => {}}} => {"base"=>{"logstash-forwarder"=>{}}} [124] pry(main)> path = nil => nil [125] pry(main)> e.result binding => " \n" [126] pry(main)>  

so problem either chef weird (seems unlikely) or node isn't nil? think is.

update

reading between lines, @ .each , .last calls, possible node['base']['logstash-forwarder']['nginx'] not nil rather []?

if so, change .nil? check .empty?


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -