たれみみマンデー

【Rails5】ActionCableチャットコード

汚いのはお許しを・・・以下ソースだけ。

app/assets/javascripts/channels/product.coffee

jQuery(document).on 'turbolinks:load', ->

open_messages = $('#open_messages')

if $('#open_messages').length > 0
  if (!App.product)
   App.product = App.cable.subscriptions.create { channel: "ProductChannel",product_id: 
   open_messages.data('product_id')},
   connected: ->


   disconnected: ->

   received: (data) ->

   $('#open_messages').append '<div>' + data['open_message'] + '</div>'


   open_chat: (open_message) ->
   @perform 'open_chat', {open_message: open_message} #, seller_name: current_seller.name


  $(document).on 'click', '.comment_button', (event) ->

   text = $('[data-behavior~=open_chat_input]').val()

 if (text != "")
   App.product.open_chat text
   $('[data-behavior~=open_chat_input]').val("")
   event.preventDefault()

app/channels/product_channel.rb

class ProductChannel < ApplicationCable::Channel
  def subscribed
    stream_from "product_channel_#{params['product_id']}"
  end

  def unsubscribed

  end

  def open_chat(data)
    OpenMessage.create!(product_id: params['product_id'],sent_user: current_user.id,content: 
data['open_message'])
    OrderMailer.open_chat(params['product_id'],current_user.id).deliver_now
   # ActionCable.server.broadcast 'product_channel',{content: data['open_message']}
  end
end

 
 

app/jobs/***broadcast_job.rb

class OpenMessageBroadcastJob < ApplicationJob
  queue_as :default

  def perform(open_message)
    ActionCable.server.broadcast "product_channel_#{open_message.product_id}", {open_message: 
    render_message(open_message)}
  end

private
 def render_message(open_message)
   ApplicationController.renderer.render(partial: 'open_messages/open_message', locals: { open_message: open_message })
  end
end


app/views/**.rb

<% if signed_in? %>
  <input type="hidden" name="current_user_name" value="<%= current_devise_user.id %>" 
class="current_user_name">
<% end %>
<div id="open_messages", data-product_id="<%= @product.id %>">
  <%= render partial: @open_messages %>
</div>

<form>
  <label>コメント投稿:
    <% if signed_in? %> 
      <%= button_tag type: 'button', class: "comment_button" do %>
        <i class="fa fa-paper-plane" aria-hidden="true"></i>
      <% end %>
    <% end %>
    <input type="text" data-behavior="open_chat_input" class="open_chat"></>
  </label>
</form>

AWS+Nginx+RailsのSSL化

qiita.com

ここの続きのテイ

自分用備忘録。

 

$cd ~

$git clone https://github.com/certbot/certbot

$cd certbot

 

$sudo -i

 

$sudo ./certbot-auto certonly --agree-tos --webroot -w /var/www/rails/*アプリ名*/public -d hhbox.net -m *メアド --debug

 

$/etc/nginx/nginx.conf

root /var/www/rails/*アプリ名*/public

 

$/etc/nginx/conf.d/*アプリ名*.conf

server { 

  listen 80;

  server_name hhbox.net;

  return 301 https://$host$request_uri;

}

server {

  listen 443 ssl;

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

  ssl on;

  ssl_certificate      /etc/letsencrypt/live/hhbox.net/fullchain.pem;

  ssl_certificate_key  /etc/letsencrypt/live/hhbox.net/privkey.pem;

  location @app {

    proxy_set_header X-Forwarded-Proto $scheme;

  }

}

 

$config/environments/production.rb

config.force_ssl = true  

 

$sudo -i

$/etc/init.d/nginx restart

 

AWSでSSL化する

AWSSSL化するのにCertificateManegerだーメールサーバーが必要だーSESだーS3だーってややこしすぎて困ってたらとても簡単な記事見つけて助かりました!備忘録

www.wantedly.com

aws.amazon.com

特定コントローラーのアクションにbefore_actionの例外をつける

schoolコントローラーindexアクションのみbefore_actionでnot_registered_schoolをスキップしたいとき(ややこしい、、、)

 

class ApplicationController < ActionController::Base

before_action : not_registered_school,except: [:index] ,if: :school_controller? 

def school_controller?

  self.controller_name == "school"

end

def not_registered_school

  *******

end

 

たれみみ (@taremimi_7)