2

I have a change avatar popup in my ruby on rails application. I'm trying to use it for several models like changing profile picture or picture of a product.

I have created a view and trying to open it with parameters as follows in the user.html.erb file:

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

In my _index.html.erb file which is located under views/change_avatar:

<h1><%= par %></h1>

But, I get "undefined local variable or method `par' for" when I open user.html.erb file.

I can't see what I'm missing.

Any suggestions ?

Thanks.

1
  • Can you show the output of the web console? Should tell you if it doesn't find a value or declaration of par in the partial or the parent file. Where the error is happening in the chain should tell you more about what the problem is. Commented Nov 21, 2017 at 22:14

1 Answer 1

6

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] %>

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

local_assigns[:par] seems to be the key. Thanks a lot
Thanks for this answer! It's 2020 and I have this problem with a Rails 6 app. I had to modify the code slightly: <% name ||= local_assigns[:locals][:name] %>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.