eth3rnit3
eth3rnit3

Reputation: 801

Rails 5.1 locals and partials

in my Rails application, I wanted to create a toolbox that is available on several places of the application but with different parameters and data. To do this I created a partial that I call when I need, my problem is that if I call the partial several times on the same page, the variables are override and instead of having different data in each partial the last call override all the others locals variables

example : On the same page : 1st call :

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

2nd call :

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

It is two call that finds itself in different biased called on the same page

My 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}

Example of tool_box call :

= 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"}

Upvotes: 1

Views: 168

Answers (1)

eth3rnit3
eth3rnit3

Reputation: 801

the problem came from my content_for I had to add the flush: true option to overwrite the cache

Upvotes: 1

Related Questions