Yield and default case || do not output default case

前端 未结 7 1233
刺人心
刺人心 2021-02-14 15:49

I have a simple yield use case and for some unknown reason the default case is never shown:

In my super_admin layout I have:

<%= yield :body_id || \'s         


        
相关标签:
7条回答
  • 2021-02-14 16:31

    Use parentheses:

    <%= (yield :body_id) || 'super_admin_main' %>
    

    Or

    <%= yield(:body_id) || 'super_admin_main' %>
    

    Without them it is assuming yield (:body_id || 'super_admin_main')

    EDIT: Rails 3 uses ActiveSupport::SafeBuffer instead of string/nil (Rails 2), so the output is not nil even if there is no content_for provided. So try:

    <%= yield(:body_id).empty? ? 'super_admin_main' : yield(:body_id)%>
    
    0 讨论(0)
提交回复
热议问题