mardi 24 janvier 2017

Append ruby partial in javascript, and pass a rails variable

Creating a chat component with ActionCable in Rails5. I learned today that render partial, even in .js.erb does not work. render specifically is the issue. So now I'm trying to call a partial, and pass a ruby variable to it.

I have tried many things, currently this is how I'm trying to accomplish it:

chatbox.append(<%= j (Erubis::Eruby.new(File.read(File.join(Rails.root, 'app/views/msgs',
        '_msg.html.erb'))).result(msg: @msg)) %>);

and the error is

 undefined method `self_or_other' for #<Erubis::Eruby:0x007f832bbb8590>

So the partial is there, but the variable is not correct.


views/msgs/_msg.html.erb

<li class="<%=  self_or_other(msg) %>">
  <div class="avatar">
    <%# if msg_interlocutor(msg).avatar.url(:thumb) == "thumb/missing.png" %>
    <%= image_tag(msg_interlocutor(msg).avatar.url(:thumb), class: "convoPic")%>
</div>
</li>

I also tried:

chatbox.append(<%= j (Msg.new(File.read(File.join(Rails.root, 'app/views/msgs',
        '_msg.html.erb'))).result(msg: @msg)) %>);

And get this error: When assigning attributes, you must pass a hash as an argument.

How do I properly call in a partial, pass it a ruby variable (with the msg record). Thank you!!

Aucun commentaire:

Enregistrer un commentaire