在rails上的ruby中进行对话的搜索功能

时间:2016-05-24 13:36:20

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

我是铁杆新手。我想为我的rails应用程序添加搜索功能。用户可以搜索他与其他用户进行的对话。在该搜索中,他可以键入消息的关键字或他聊天的用户名。有人可以指导我完成这个......

conversation.rb是,

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是,

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是,

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是,

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

有人可以指导我编写搜索功能并查看搜索结果。我试过两种不同的类型,但没有工作。提前致谢

1 个答案:

答案 0 :(得分:0)

在对话模型或任何你命名的模型中,都会添加类似这种搜索方法的内容:

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

然后在索引中有这样的东西:

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

最后在控制器中有这样的东西:

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

观看此视频,以便更好地了解在导轨中搜索:
http://railscasts.com/episodes/37-simple-search-form
http://railscasts.com/episodes/111-advanced-search-form
http://railscasts.com/episodes/240-search-sort-paginate-with-ajax
https://www.youtube.com/watch?v=iMz8HmrQ350
https://www.youtube.com/watch?v=n41F29Qln5E
http://railscasts.com/episodes/306-elasticsearch-part-1?autoplay=true
https://www.youtube.com/watch?v=GJCrcSO-zcc
http://railscasts.com/episodes/278-search-with-sunspot

注意第一个是我认为最适合你的。这是一个简单的搜索,它很适合你所说的。