Include Coffeescript File with ERB in View

It gives me a serious headache ...

So, I have an application that requires a sidebar that lists various user information with the user. One section of this sidebar is the friends list. Now that Player A sends a request to a friend of Player B, the request should be automatically registered in the sidebar of B, and I intend to use WebSockets for this.

Here is my file cp.js.coffe.erb(at the moment there are only a few ERB fragments, more downloads, and I will probably do it first):

$ ->
  $("#cp").accordion()

  if `"WebSocket" in window`
    socket = new WebSocket("ws://localhost:8080") 

  socket.onopen = =>
    console.log("Connection Open")
    init = {
      sender: "cp"
      action: "init"
      user:   <%= @user.id %>
      token:  <%= cookies["remember_token"] %>
    }

    socket.send(init.to_json)

  socket.onerror = (e)=>
    console.log(e)

  socket.onclose = =>
    console.log("Closed")

  socket.onmessage = (m)=>
    console.log("Recieved: #{m.data}")
    msg = m.data.JSON.parse
    switch msg.action
      when "ret_init"

      when "friend_udt"
        refreshFriend()

refreshFriend() ->
  html = "<%= j render 'layouts/friends' %>"
  $('#friends').empty()
  $('#friends').add(html)

Theoretically, the code itself works fine, the problem is that Rails does not allow ERB to be used in the asset pipeline, so this file must be in app/views/layouts. the file cannot access the variables declared in the controller or use the method render(or most other ERB methods).

Here's the thing: I can’t include the specified file in my file application.html.erb, and I looked into the file request with AJAX, but from my understanding that I will immediately run Javascript once and once, and I need methods in this to constantly update the sidebar .

Is there a way to include this file so that it works with ERB and CoffeScript so that it is always available for this page? Don't I understand the whole AJAX request method?


Thanks to @nzifnab for helping with JS. Now my friends partially look like this:
<ul id="friendlist">
    <% if Relation.find_by(owner: @user.id, type: "freq") != nil %>
        <% Relation.find_by(owner: @user.id, type: "freq").each do |r| %>
            <li class="friend-request-cp"><%= link_to "/#{User.find(r.character).name}" %></li>
        <% end %>
    <% end %>
    <% if Relation.find_by(owner: @user.id, type: "friend") != nil %>
        <% Relation.find_by(owner: @user.id, type: "friend").each do |r| %>
            <li class="friend-cp"><%= link_to "/#{User.find(r.character).name}" %></li>
        <% end %>
    <% end %>
</ul>

, ERB. , , , , - . , . ? hamlcoffeeassets, ?


A slight tangent appears:
By the way, I'm using Ruby 2.0.0-p247 and Rails 4 on Windows 7 . I felt the need to enable this because of some serious gemstone compatibility issues that are very different from Ubuntu. I had to switch from Ubuntu to Windows because updating from 13.04 to 13.10 broke all Ruby Gem on this OS. I do not have a tome to find a fix: I literally have only four days to create this application.
+4
source share
3 answers

erb , , , , , @user ( ), . erb , , , JS. , .

, , javascript cookie , ( , ). , HTML- DOM javascript .

cookie: https://github.com/carhartl/jquery-cookie

, Google.

socket.onopen = =>
  console.log("Connection Open")
  init = {
    sender: "cp"
    action: "init"
    user:  $.cookie('user_id')
    token:  $.cookie('remember_token')
  }

JS. - js-. hamlcoffeeassets : https://github.com/netzpirat/haml_coffee_assets haml , ERB. ERB.

app/assets/templates/friend.jst.hamlc, :

%p This is my friend markup #{@friend.name}

JS :

$('#friends').append(JST['templates/friend'](friend: {name: 'Bob'}))

, . #friends:

<p>This is my friend markup Bob</p>

, , JSON , ...

, JS :

socket.onmessage = (m)=>
  console.log("Recieved: #{m.data}")
  msg = m.data.JSON.parse
  switch msg.action
    when "ret_init"

    when "friend_udt"
      refreshFriend(msg.friendHTML)

refreshFriend(html) ->
  $('#friends').html(html)

UPDATE

ERB... , , , . haml_coffee_assets haml ( ERB), , ERB eco: https://github.com/sstephenson/eco

JS, " " JSON , javascript partials - , , javascript .

+3

app/assets/javascripts/cp.js.coffee.erb, erb (. ) , coffee, !

, ajax , /assets/cp.js.

0

try this stone: ' coffeebeans '

  • name your coffee file as "some_file.html.erb"

    <%= coffeescript_tag_do %> 
        # your coffee script here ...
    <% end %>
    
  • in another erb file:

    <%= render file: '.../some_file' %>
    
0
source

All Articles