vendredi 3 juin 2016

search function for mails checking in ruby on rails

I m new to ruby on rails. i m working on search function in the gem 'mailboxer' that searches for keywords in the inbox and outbox of the mails in the system. After entering the keywords in the search box, it is not giving the results of the search function. may be i have written code wrong somewhere. please check the code Conversation_controller.rb is,

class ConversationsController < ApplicationController


 before_action :authenticate_user!

  def new
  end

  def create
    recipients = User.where(id: conversation_params[:recipients])
    conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
    flash[:success] = "Your message was successfully sent!"
    redirect_to conversation_path(conversation)
  end

  def show
    @receipts = conversation.receipts_for(current_user)
    # mark conversation as read
    conversation.mark_as_read(current_user)
  end
  def reply
    current_user.reply_to_conversation(conversation, message_params[:body])
    flash[:notice] = "Your reply message was successfully sent!"
    redirect_to conversation_path(conversation)
  end
  def trash
    conversation.move_to_trash(current_user)
    redirect_to mailbox_inbox_path
  end

  def untrash
    conversation.untrash(current_user)
    redirect_to mailbox_inbox_path
  end

  def search
  @conversation= Conversation.search(params[:search])
 end

  private

  def conversation_params
    params.require(:conversation).permit(:subject, :body,recipients:[])
  end

   def message_params
    params.require(:message).permit(:body, :subject)
  end
end

mailbox_controller.rb

    class MailboxController < ApplicationController
  before_action :authenticate_user!

  def inbox
    @inbox = mailbox.inbox
    @active = :inbox
  end

  def sent
    @sent = mailbox.sentbox
    @active = :sent
  end

  def trash
    @trash = mailbox.trash
    @active = :trash
  end

end

user.rb is,

    acts_as_messageable

def mailboxer_name
  self.name
end

def mailboxer_email(object)
  self.email
end

def self.search(search)
  if search
  where(['fullname LIKE ? or subject LIKE ?', "%#{search}%","%#{search}%"])
  end
end

end in _folder_view.html.erb

<%= form_tag mailbox_search_path, :method => 'get' do %>   
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", name: nil %>
  <% end %>

search.html.erb

<%= render partial: 'mailbox/folder_view', locals: { is_conversation: false, messages: @search } %>

routes.rb is,

get "mailbox/inbox" => "mailbox#inbox", as: :mailbox_inbox


 get "mailbox/sent" => "mailbox#sent", as: :mailbox_sent
  get "mailbox/trash" => "mailbox#trash", as: :mailbox_trash
  get "mailbox/search" => "mailbox#search", as: :mailbox_search

  resources :conversations do
    member do
      post :reply
      post :trash
      post :untrash
    end
  end

I would like to give the user a search option for checking his inbox and sent. can some one guide me through this. thanks in advance.

Aucun commentaire:

Enregistrer un commentaire