How does local_assigns work in Rails?

前端 未结 1 1682
臣服心动
臣服心动 2021-01-31 07:14

I\'ve been googling around about this and can\'t find the right path. I\'m working on a Rails app that is using a method called local_assigns. This appears to be something in Ra

相关标签:
1条回答
  • 2021-01-31 07:51

    local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not.

    Here you render a partial with some values, the headline and person will become accessible with predefined value.

    <%= render "shared/header", { :headline => "Welcome", :person => person } %>
    

    In shared/header view:

    Headline: <%= headline %>
    First name: <%= person.first_name %>
    

    Here is how you check these variables has passed in or not:

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

    Check this document for more detail on the section Passing local variables to sub templates.

    0 讨论(0)
提交回复
热议问题