0

I want to push to websockets what normally would be returned from a jbuilder view when the route is called but I cannot call render twice in an action:

def
  ...

  @resource_receivers = "#{@resource.model_name.singular.capitalize}Receiver".constantize.where("#{@resource.model_name.singular}_id": @resource)
  @resource_receivers.each do |resource_receiver|
    resource_receiver.create_activity :sent, owner: @current_user, recipient: resource_receiver.receiver
  end

  to_return = render 'api/received/show'

  Pusher.trigger('resources', 'resource.received', JSON.parse(to_return.first))

  return render :task
end

How would I solve this? Or in general, I'd like to return what normally would be returned from the jbuilder anywhere else in my application, for example if I wanted to refactor this to have a method that pushes to the user

Mohamed El Mahallawy
  • 13,024
  • 13
  • 52
  • 84

1 Answers1

0

If you want to render a view and assign it to a variable instead of setting it as the response body, then you can use render_to_string.

Will Sewell
  • 2,593
  • 2
  • 20
  • 39