samedi 18 juin 2016

How to send a redirect link as a message to user

I have a button that sends the current user to a link. I want the same link to be sent as a message to another user. I'm using Mailboxer for the messaging part.

enter image description here

Button in my show.html.erb:

<%= form_tag zoom_index_path do %>
  <%= submit_tag 'Start a session' %>
<% end %>

Controller for the button:

class ZoomController < ApplicationController
  require 'zoomus'

  def index
  end

  def create
    zoomus_client = Zoomus.new
    meeting = zoomus_client.meeting_create(host_id: 'dWkVNB2tQvic4YcPYK6tDA', topic: 'topic1', type: 1)
    redirect_to meeting["start_url"]


end

Message controller:

class MessagesController < ApplicationController
  before_action :authenticate_user!

  def new
    @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
  end

  def create
    recipients = User.where(id: params['recipients'])
    conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
    flash[:success] = "Message has been sent!"
    redirect_to conversation_path(conversation)
  end
end

Conversation controller: edited to shorten code.

class ConversationsController < ApplicationController
  before_action :authenticate_user!
  before_action :get_mailbox
  before_action :get_conversation, except: [:index, :empty_trash]
  before_action :get_box, only: [:index]

  def index
    if @box.eql? "inbox"
      @conversations = @mailbox.inbox
    elsif @box.eql? "sent"
      @conversations = @mailbox.sentbox
    else
      @conversations = @mailbox.trash
    end

    @conversations = @conversations.paginate(page: params[:page], per_page: 10)
  end


  def show
  end

  def reply
    current_user.reply_to_conversation(@conversation, params[:body])
    flash[:success] = 'Reply sent'
    redirect_to conversation_path(@conversation)
  end

  private

  def get_mailbox
    @mailbox ||= current_user.mailbox
  end

  def get_conversation
    @conversation ||= @mailbox.conversations.find(params[:id])
  end

  def get_box
    if params[:box].blank? or !["inbox","sent","trash"].include?(params[:box])
      params[:box] = 'inbox'
    end
    @box = params[:box]
  end
end

Aucun commentaire:

Enregistrer un commentaire