Hope I can phrase this question properly as I'm also getting a quite confused with partials. I have the following files:
./body.html.erb
./_sidebar.html.erb
Essentially, I'd like to paste all sidebar info into the body. It repeats itself quite a few times (in a few tabs) so I found a few resources saying that render partial is the correct way to re-call multiple blocks of code.
./body.html.erb
looks like this: (essentially member_list returns data of users like username, image, etc as seen on the sidebar. It definitely has content in it and everything works when it is all within the body.html.erb
file)
<div >
<%= render partial: "sidebar", locals: { members: member_list } %>
</div>
./_sidebar.html.erb
looks like this:
<div >
<div >
Members(<%= local_assigns[:members].count() %>)
</div>
<div >
<% local_assigns[:members].slice(0, 20).each do |user| %>
<div >
<img width="46" height="46" src=<%= user.avatar_template.gsub('{size}', (46).to_s) %> class='avatar'>
</div>
<% end %>
</div>
</div>
Here are my questions:
What is causing my code not to work? Unfortunately I don't get any errors because of nginx (just the standard 500 error), but it works when I have the
sidebar-content
insidebody.html.erb
and replacinglocal_assigns[:members]
tomember_list
.How do I declare multiple variables to pass on? Would it be like so:
<%= render partial: "sidebar", locals: { members: member_list , members_check: members_exist} %>
- How do I declare
openTab
in the locals of render partial if I adde the below code into the_sidebar.html.erb
?
<div >
<a onclick="openTab(event, 'Members')">see members <i ></i> </a>
</div>
TLDR
This works (body.html.erb
):
<div >
<div >
<div >
Members(<%= member_list.count() %>)
</div>
<div >
<% member_list.slice(0, 20).each do |user| %>
<div >
<img width="46" height="46" src=<%= user.avatar_template.gsub('{size}', (46).to_s) %> class='avatar'>
</div>
<% end %>
</div>
</div>
This doesn't (when separated into 2 files):
./body.html.erb
<div >
<%= render partial: "sidebar", locals: { members: member_list } %>
</div>
./_sidebar.html.erb
:
<div >
<div >
Members(<%= local_assigns[:members].count() %>)
</div>
<div >
<% local_assigns[:members].slice(0, 20).each do |user| %>
<div >
<img width="46" height="46" src=<%= user.avatar_template.gsub('{size}', (46).to_s) %> class='avatar'>
</div>
<% end %>
</div>
</div>
CodePudding user response:
Ruby on Rails will always look in the current controller's view folder for a partial unless the partial name is prefixed with another folder name.
The official Rails Guides suggest placing shared partials in app/views/shared
and then specifying that path when including the partial.
Place the partial at app/views/shared/_sidebar.html.erb
and change the call in the body to this:
<div >
<%= render partial: "shared/sidebar", locals: { members: member_list } %>
</div>