Rails 5.1 locals和partials

时间:2018-04-04 12:18:58

标签: ruby-on-rails haml partial-views

在我的Rails应用程序中,我想创建一个工具箱,该工具箱可在应用程序的多个位置使用,但具有不同的参数和数据。 为了做到这一点,我创建了一个我需要的部分,我的问题是,如果我在同一页面上多次调用部分,变量是覆盖,而不是在每个部分中有不同的数据,最后一个调用覆盖所有其他本地变量

示例: 在同一页上 : 第一次电话:

= render partial: "layouts/dynamic_tool_box/dynamic_tool_box", locals: {pagined_datas: members, class_name: "company_team"}
# Here, class_name = "company_connexion_history"

第二次电话:

= render partial: "layouts/dynamic_tool_box/dynamic_tool_box", locals: {pagined_datas: @connexion_histories, class_name: "company_connexion_history"}

这是两个调用,发现自己处于同一页面上的不同偏见

我的tool_box_template:

.dynamic_tool_box
  .tool_box
    .flexboxed-between
      %h2{class: "tool_box_title"}
        = "Plus d'Outils"
      %h2{class: "develop_dynamic_tool_box"}
        = "+"
    .dynamic_tool_box_content
      = render partial: "layouts/dynamic_tool_box/dynamic_tool_box_more", locals: {class_name: class_name}
    .dynamic_tool_search
      = render partial: "layouts/dynamic_tool_box/dynamic_tool_box_search", locals: {class_name: class_name}
    .end_of_tool_box

  = yield :dynamic_table
  .row
    .col-12
      .pagination.flexboxed-centered
        = will_paginate pagined_datas
        %p{style: "position: absolute; margin-top: -24px;"} 
          = "Affichage de #{CustomMethods::MetalsIndustry.return_per_page_or_default(@current_user, class_name)} / #{pagined_datas.count} éléments"
  .row
  .tool_box
    .dynamic_tool_search
      = render partial: "layouts/dynamic_tool_box/dynamic_tool_box_search", locals: {class_name: class_name}
    .flexboxed-between{style: "margin-top: 30px;"}
      %h2{class: "tool_box_title"}
        = "Plus d'Outils"
      %h2{class: "develop_dynamic_tool_box"}
        = "+"
    .dynamic_tool_box_content
      = render partial: "layouts/dynamic_tool_box/dynamic_tool_box_more", locals: {class_name: class_name}

tool_box调用示例:

= content_for :dynamic_table do 
  #person-connexion-history
    %table.pretty.table{style: "border: 1px solid #E89F3A; color: gray;"}
      %thead
        %tr{style: "border: 1px solid #E89F3A;"}
          %th{style: "text-align: center;"}
            = sortable_employe "family_name", t("company.form.members.lastname")   
          %th{style: "text-align: center;"}
            = sortable_employe "given_name", t("company.form.members.firstname") 
          %th{style: "text-align: center;"}
            = sortable_employe "email_perso", t("company.form.members.email") 
          %th{style: "text-align: center;"}
            = sortable_employe "phone_number", t("company.form.members.phone") 
          %th{style: "text-align: center;"}
            = t("company.form.members.access") 
      - if members
        - members.each do |member|
          %tr
            %td{style: "text-align: center;"}
              = member.family_name
            %td{style: "text-align: center;"}
              = member.given_name 
            %td{style: "text-align: center;"}
              = member.email_perso
            %td{style: "text-align: center;"}
              = member.phone_number
            %td{style: "text-align: center;"}
              = link_to('Voir', {})

-# Ici on apelle le partial qui attend un content_for :dynamic_table et on lui donne les données a paginer
= render partial: "layouts/dynamic_tool_box/dynamic_tool_box", locals: {pagined_datas: members, class_name: "company_team"}

1 个答案:

答案 0 :(得分:1)

问题来自我的content_for我必须添加flush: true选项来覆盖缓存