I have a function that simple selects all of the checkboxes on the current page, creates a form, and submits it automatically.
Instead of repeating the same exact JavaScript code on every single view, I want to try to just have a shared partial.
Here's all I have thus far:
#views/shared/_delete_multiple_items.js.erb
alert("<%= random_variable %>");
.
#views/users/index.html.erb
...
<script>
function deleteMulitpleUsers() {
<%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World" %>
}
</script>
It looks like Rails is trying to render the partial in HTML though, if I understand this error correctly:
Missing partial shared/_delete_multiple_items with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
I've tried to include :formats => :js
with render, like this:
<%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World", :formats => :js %>
but I still get the same exact error.
I have a function that simple selects all of the checkboxes on the current page, creates a form, and submits it automatically.
Instead of repeating the same exact JavaScript code on every single view, I want to try to just have a shared partial.
Here's all I have thus far:
#views/shared/_delete_multiple_items.js.erb
alert("<%= random_variable %>");
.
#views/users/index.html.erb
...
<script>
function deleteMulitpleUsers() {
<%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World" %>
}
</script>
It looks like Rails is trying to render the partial in HTML though, if I understand this error correctly:
Missing partial shared/_delete_multiple_items with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
I've tried to include :formats => :js
with render, like this:
<%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World", :formats => :js %>
but I still get the same exact error.
Share Improve this question edited Aug 10, 2022 at 23:14 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Sep 2, 2018 at 21:49 LewlSauceLewlSauce 5,89213 gold badges62 silver badges120 bronze badges3 Answers
Reset to default 5Change this:
<%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World" %>
to this:
<%= render partial: 'shared/delete_multiple_items', formats: :js, :locals => {:random_variable => "Hello World"} %>
partial:
at the beginning to tell rails to look for a partial and formats: :js
to tell rails to look for a js file (note that formats
is NOT a child of locals
)
You get that error because server responding format is html, not javascript. Setting response format is done in controller so putting format: :js
in templates won't work. Try these steps and see if they work for you.
Rename
_delete_multiple_items.js.erb
to_delete_multiple_items.html.erb
Put
alert("<%= random_variable %>");
inside<script>
tagIn
index.html.erb
, remove all js related code and just do<%= render 'shared/delete_multiple_items', :locals => {:random_variable => "Hello World" %>
Have you tried adding a j on your js.erb file?
#views/shared/_delete_multiple_items.js.erb
alert("<%= j random_variable %>");