I have a Rails 5 app using webpacker that displays a list of users. The logic to do so is in a partial that has Vue component code in it, like
<profile_modal>
<template slot="trigger">
<figure class="profile-card__figure | image -is1x1">
<img src="<%= person.avatar.url %>" alt="">
</figure>
...
Everything works well, and the .erb changes this correctly to HTML when the page is processed. So far so good.
Now I've added a button that says "show more users" that makes an ajax call to pull in more users. I've made a js.erb that has in it:
$("#users").html("<%= escape_javascript(render partial: 'layouts/components/profile-card--modal', collection: @users.last(12), as: :person, locals: { content: @users_presenter.profile_modal }) %>");
This code is executed, but instead of converting the "profile_modal" to the right html, rails is just inserting it right in the page, which makes everything wrong.
Is there a way I can have the vue code be transpiled into the html it would become during normal, non-ajaxy processing?
show more users
that makes an ajax call toyourfile.js.erb
. The file includes the following javascript$("#users").html(....)
which appends to#users
the html from partialprofile-card--modal
with the data of@users.last(12)
. This last step is not clear to me.This code is executed, but instead of converting the "profile_modal" to the right html, rails is just inserting it right in the page, which makes everything wrong.
it would be useful to see the actual effect you are describing with a screenshot or the actual source code. Thanks – Gaga<profile_modal>
is some sort of component, which javascript should replace with html but instead it gets directly appended to the page when you run$("#users").html()
. You have this problem only withasynchronous javascript request
which do not run a full page reload? This means that we can isolate the problem toWebpacker
withVue
not working correctly? You are replying with rails controller in javascript format and using an.erb.js
view. – Gaga