Skip to content

how to send custom action result to the client #238

@ffabreti

Description

@ffabreti

Hi Dennis,
In the docs, you say:

The index action will expect an instance variable with the same name as your controller in the plural form (e.g. @messages in a MessagesController)
The show, create, update, and destroy actions will expect an instance variable with the singular name of your controller (e.g. @message in a MessagesController)

What about custom actions? How do I return a collection to the client?

With the following code, I get nothing on client:
Rails:

    def since
        broadcast do
            since = chat_room_since
            @chat_rooms = []
            @chat_rooms = ChatRoom.where("last_time > ?", since).to_a if since
        end
    end

    private
    def chat_room_since
        date = URI.decode params.require(:date)
        DateTime.parse(date) if date
    end

Angular:

since: function(dateString, cback) {

        if (!sinceSocket) {
            sinceSocket = new WebSocket(BASEURL + '/chat_rooms/since');
            sinceSocket.onopen = function (data) {
                sinceSocket.send(JSON.stringify({date: dateString}));
            }
        }
        sinceSocket.onmessage = function (data) {
            if (data.err) {
                console.log('Chatrooms: create: error: ',err);
            }
            if (cback) cback(data);

        };

     },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions