mardi 24 mai 2016

search function for conversations in ruby on rails

I m new to rails. I would like to add a search function for my rails app. the user can search for the conversations that he made with other users. in that search, he can type keywords of the messages or the user name that he chatted. Can some one guide me through this...

conversation.rb is,

class Conversation < ActiveRecord::Base
belongs_to :sender, foreign_key: :sender_id, class_name: 'User'
belongs_to :recipient, foreign_key: :recipient_id, class_name: 'User'

has_many :messages, dependent: :destroy

validates_uniqueness_of :sender_id, scope: :recipient_id

scope :involving, -> (user) do
    where("conversations.sender_id = ? OR conversations.recipient_id = ?", user.id, user.id)
end

scope :between, -> (sender_id, recipient_id) do
    where("(conversations.sender_id = ? AND conversations.recipient_id = ?) OR (conversations.sender_id = ? AND conversations.recipient_id = ?)", 
                sender_id, recipient_id, recipient_id, sender_id)
end

end

message.rb is,

class Message < ActiveRecord::Base


 belongs_to :conversation
  belongs_to :user

  validates_presence_of :content, :conversation_id, :user_id

  def message_time
    created_at.strftime("%v")
  end

end

conversations_controller.rb is,

class ConversationsController < ApplicationController
before_action :authenticate_user!

def index
    @users = User.all
    @conversations = Conversation.involving(current_user)
end

def create
    if Conversation.between(params[:sender_id], params[:recipient_id]).present?
        @conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first
    else
        @conversation = Conversation.create(conversation_params)
    end

    redirect_to conversation_messages_path(@conversation)
end

private

    def conversation_params
        params.permit(:sender_id, :recipient_id)
    end

end

messages_controller.rb is,

class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation

def index
    if current_user == @conversation.sender || current_user == @conversation.recipient
        @other = current_user == @conversation.sender ? @conversation.recipient : @conversation.sender
        @messages = @conversation.messages.order("created_at DESC")
    else
        redirect_to conversations_path, alert: "You don't have permission to view this."
    end
end

def create
    @message = @conversation.messages.new(message_params)
    @messages = @conversation.messages.order("created_at DESC")

    if @message.save
        respond_to do |format|
            format.js
        end
    end
end

private

    def set_conversation
        @conversation = Conversation.find(params[:conversation_id])
    end

    def message_params
        params.require(:message).permit(:content, :user_id)
    end
end

Can someone guide me to write the functionality for search and view the results of the search. i have tried in two different types but not worked. Thanks in advance

Aucun commentaire:

Enregistrer un commentaire