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

前端 未结 12 2332
没有蜡笔的小新
没有蜡笔的小新 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:17

    Ruby 2.5

    Erb

    It's possible, but you must to declare your default values in the scope.

    VARIABLE the word for replacement.

    # index.html.erb
    ...
    <%= render 'some_content', VARIABLE: false %>
    ...
    
    # _some_content.html.erb
    ...
    <% VARIABLE = true if local_assigns[:VARIABLE].nil? %>
    <% if VARIABLE %>
        

    Do you see me?

    <% end %> ...

提交回复
热议问题