0

I have created a vew as show.html.erb where there is a list of checkboxes are present I want when a checkbox is cliked a action named as Generate is perform and then render a partial inside that show.html.erb I tried using Action cable but unable to do so here is my Show.html.erb

<%= @files.each do |key,value|%>
 <div class="subdir">
 <%= check_box_tag(key, value, false, data: {remote: true, method: :post, url: generate_repository_path }) %>
 <%= label_tag key%>
 </div>
<br>
<%end%>

generate method

  def generate
        request.POST.each do |key,value|
          @filename=key
          @path= value
        end
        @id=params[:id]
        @file=Hash.new
        if File.directory?(@path)
           Dir.chdir("#{@path}")
           Dir.glob("*").each do |entries|
             @file[entries]="#{@path}/entries"
            puts "#{entries}"
          end
          render(partial: 'operations/dir',object: @file)
          else
            puts "It is a file"
          end
      end

Here's my partial

<%= @file.each do |key,value|%>
 <%= check_box_tag(key, value, false, data: {remote: true, method: :post, class: "checkbox", url: generate_repository_path }) %>
 <%= label_tag key%>
<br>
<%end%>

here's my operations.coffee file

App.operations = App.cable.subscriptions.create "OperationsChannel",
  connected: ->
    # Called when the subscription is ready for use on the server

  disconnected: ->
    # Called when the subscription has been terminated by the server

  received: (data) ->
    # Called when there's incoming data on the websocket for this channel
    $(".subdir").prepend(data);

and Operations_channel.erb

class OperationsChannel < ApplicationCable::Channel
  def subscribed
    stream_from "operations"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

here's the log

Started GET "/cable" for 127.0.0.1 at 2017-03-03 14:37:37 +0530
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-03-03 14:37:37 +0530
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)
OperationsChannel is transmitting the subscription confirmation
OperationsChannel is streaming from operations
Started POST "/repositories/12/generate" for 127.0.0.1 at 2017-03-03 14:37:42 +0530
Processing by RepositoriesController#generate as JS


Parameters: {"sections"=>"/home/sifat/directory/public/system/repositories

/uploads/extract/12/resume/sections", "id"=>"12"}
skills.tex
work.tex
projects.tex
bio.tex
education.tex
communication.tex
  Rendered operations/_dir.html.erb (1.3ms)
Completed 200 OK in 6ms (Views: 4.1ms | ActiveRecord: 0.0ms)

I am able to call the method generate from checkbox moreover rendered partial _dir.html.erb but It is not changed or render in the show.html.erb.

Amitoj Singh
  • 121
  • 1
  • 1
  • 7

1 Answers1

0

Your checkbox click handler on the server side should be triggering an ActionCable broadcast: http://api.rubyonrails.org/classes/ActionCable/Server/Broadcasting.html

I would also suggest debugging with some console logs on the connected and received methods on your Operations Channel file "operations.coffee"

J. S.
  • 8,905
  • 2
  • 34
  • 44