【问题标题】:search function for conversations in ruby on railsruby on rails 中的对话搜索功能
【发布时间】:2016-05-24 13:36:20
【问题描述】:

我是 Rails 新手。我想为我的 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

谁能指导我编写搜索功能并查看搜索结果。我尝试了两种不同的类型,但没有奏效。提前致谢

【问题讨论】:

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


    【解决方案1】:

    在对话模型或您所命名的任何内容中,放置类似以下搜索方法的内容:

    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])
    

    观看此视频以更好地了解在 Rails 中的搜索:
    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=truehttps://www.youtube.com/watch?v=GJCrcSO-zcc
    http://railscasts.com/episodes/278-search-with-sunspot

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

    【讨论】:

    猜你喜欢
    • 2018-09-04
    • 2017-09-02
    • 2021-10-30
    • 2010-11-05
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    相关资源
    最近更新 更多