1

I'm currently having a problem while using Private Pub in a Rails 3 Project. All the examples show the usage of publish/subscribe to a route like this: "/messages/new" But What If the intended route is a little more complex, ie.

routes.rb

resources :messages, only: [] do
  post 'chat', on: :member  
end

Which will create the following

chat_message POST /messages/:id/chat(.:format) messages#chat

Therefore, I'm using it like this:

view:

= subscribe_to "/messages/#{@cid}/chat"

coffee response view:

<% publish_to "/messages/#{current_user.convos.first.id}/chat" do %>
$('#message-display').append("<%= j render partial: 'shared/single_message', locals: { user: current_user } %>")
<% end %>

But the block will never get triggered! It's supposed to be the same route, right? I mean, the segment key matches... It won't work in any views at all, the code inside the block is never reached. What am I doing wrong?

Please help me!

Jose.

Note. All the examples I've seen seem to showcase the use of this gem for a general chat. My intention, is to create various channels, for various peer-to-peer private conversations (Like Facebook chat)

jlstr
  • 2,986
  • 6
  • 43
  • 60
  • I've managed to get around this problem by using a js.erb view instead. Which has lead me to believe the problem is in how coffeeScript handles ruby blocks. can a .js.coffee view even do it? – jlstr Jun 20 '13 at 17:32
  • 1
    CoffeeScript doesn't handle ruby blocks. They're **ruby** blocks. And you can't chain extension in the `views/` folder. – Ven Jun 21 '13 at 16:02
  • So, basically the only way around it is to do what I did? – jlstr Jun 24 '13 at 14:47
  • Currently, yes. I think Rails4 allows for multi-extensions – Ven Jun 24 '13 at 14:53
  • Alright, thanks a lot sir. If you like you can post an answer, and I'll accept it, so that I can close this Question properly. :) Thanks a bunch. – jlstr Jun 24 '13 at 14:57
  • @JoseE could you please give a detailed explanation on how you solved this problem? Thanks! – Cristiano Sousa Jan 13 '14 at 22:38
  • @CristianoSousa sure, basically you need to change your view extension from: js.coffee to .js.erb, so that it can actually process a ruby block, something like this: https://gist.github.com/jlstr/8428167. The rest would the same as the snippets I originally posted. Let me know If you need any more help! – jlstr Jan 14 '14 at 23:43
  • @JoseE where is this `js.erb`file located? In the controller, do you publish the message using `PrivatePub.publish_to`? – Cristiano Sousa Jan 15 '14 at 19:29
  • @CristianoSousa, The idea is that you Publish a piece of JS to an X number of clients looking at a View at the same time. the piece of JS(.js.erb file) code goes in app/views/, it follows the Rails MVC. No, I don't publish using publish_to; As I said, you just need to use subscribe_to method in the View you assume the X number of clients will be looking at the same time. – jlstr Jan 16 '14 at 00:48

1 Answers1

2

CoffeeScript doesn't handle ruby blocks. They're ruby blocks. And you can't chain extension in the views/ folder.

Ven
  • 19,015
  • 2
  • 41
  • 61
  • Great, this is good to know, because the documentation doesn't clearly state it. thank you sir! – jlstr Jun 24 '13 at 15:09