Ruby on Rails 呈现部分未定义的局部变量

Hil*_*mcı 2 ruby ruby-on-rails partial-views partial

我的 ruby​​ on rails 应用程序中有一个更改头像弹出窗口。我正在尝试将它用于多种模型,例如更改个人资料图片或产品图片。

我创建了一个视图并尝试在 user.html.erb 文件中使用如下参数打开它:

<div id="myModal_change_avatar" class="modal-dialog fade-scale">
 <div class="modal-content">
  <div style="height: 100%;width: 100%" id="id_model_content">
   <%= render partial: 'change_avatar/index', locals: { par:'param'} %>
  </div>
 </div>
</div>
Run Code Online (Sandbox Code Playgroud)

在我的 _index.html.erb 文件中,它位于 views/change_avatar 下:

<h1><%= par %></h1>
Run Code Online (Sandbox Code Playgroud)

但是,当我打开 user.html.erb 文件时,我得到“未定义的局部变量或方法‘par’”。

我看不到我错过了什么。

有什么建议 ?

谢谢。

Dav*_*vid 5

I have also experienced this issue in the past and not entirely sure what causes it but in my case, it seemed to be related to deeply nested partials (but only for a few views).

The solution is to access the local variable from the local_assigns hash. What I tend to do is assign it to a local variable at the top of the page as so:-

<% par ||= local_assigns[:par] %>
Run Code Online (Sandbox Code Playgroud)

Hope this helps.