optional local variables in rails partial templates: how do I get out of the (defined? foo) mess?

前端 未结 12 2260
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 04:41

I\'ve been a bad kid and used the following syntax in my partial templates to set default values for local variables if a value wasn\'t explicitly defined in the :locals has

12条回答
  •  失恋的感觉
    2020-12-04 05:19

    I think this should be repeated here (from http://api.rubyonrails.org/classes/ActionView/Base.html):

    If you need to find out whether a certain local variable has been assigned a value in a particular render call, you need to use the following pattern:

    <% if local_assigns.has_key? :headline %>
      Headline: <%= headline %>
    <% end %>
    

    Testing using defined? headline will not work. This is an implementation restriction.

提交回复
热议问题