Rails 5 Action电缆无法正常工作

时间:2017-03-03 09:32:43

标签: ruby-on-rails-5 actioncable

我已经创建了一个show.html.erb,其中有一个复选框列表,我希望当一个复选框被cliked一个名为Generate的动作执行,然后在show.html.erb中渲染一个部分我尝试使用Action电缆但无法这样做 这是我的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%>

生成方法

  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

这是我的部分

<%= @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%>

这是我的operations.coffee文件

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);

和Operations_channel.erb

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

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

这里是日志

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)

我可以从复选框中调用方法generate而且渲染部分_dir.html.erb但是它在show.html.erb中没有更改或渲染。

1 个答案:

答案 0 :(得分:0)

服务器端的复选框点击处理程序应该触发ActionCable广播:http://api.rubyonrails.org/classes/ActionCable/Server/Broadcasting.html

我还建议使用操作频道文件“operations.coffee”上的已连接已接收方法调试一些控制台日志