D
D
dexdev2014-10-08 21:37:20
Ruby on Rails
dexdev, 2014-10-08 21:37:20

How to properly update object in coffee script?

In general, I’m sawing the notification system and I won’t finish it in any way! :0
Everything works, except that I can't update new notifications in live mode
. I use private_pub and can't figure out how to correctly pass the object to the coffee script
. We have the following
aplication_controller.rb

before_filter :activity

def track_activity(user, trackable, action = params[:action], author = current_user)
    Activity.create! user: user, trackable: trackable, action: action, author: author
     PrivatePub.publish_to("/messages/new", cool: activity)
  end

  def activity
    if signed_in?
      @activities = current_user.activities.includes(:trackable).order(created_at: :desc)
    end
  end
end

layout/header.html.erb
<div class="user-notification">
          <a class="fa fa-life-ring fa-lg" href="javascript:void(0);" tabindex="1"></a>
          <div class="activity activity_open" id="activity">
            <% @activities.each do |activity| %>
    <%= link_to activity.user.name, activity.user %>
    <%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %>
  
<% end %>
<%= subscribe_to "/messages/new" %>
          </div>

javascript/messages.js.coffee
P
rivatePub.subscribe "/messages/new", (data, channel) ->
alert $("#activity").append("<%= data.cool = @activities %>");

and the object object works on the alert, I understand that I need to change @activities, but I can’t figure out how. tell someone it's not difficult

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Renat Ibragimov, 2014-10-09
@MpaK999

here is the code

alert $("#activity").append("<%= data.cool = @activities %>");

you will render once, but you need it to work all the time, change it to
console.log(data); # вывести в консоль, что пришло
$('#activity').append(data.cool);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question