大家好,我正在一个有两个设计用户“家庭”和“团队”的应用程序上工作,我希望其中一个用户能够按名称搜索其他用户。我在执行此操作时遇到问题,我目前有以下代码
team.rb
class Team < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
def self.search(user_name)
if user_name
user_name.downcase!
where('LOWER(name) LIKE ?', "%#{user_name}%")
else
all
end
end
end
routes.rb
devise_for :teams, path: 'teams' , controllers: { sessions: "teams/sessions", confirmations: 'teams/confirmations', registrations: 'teams/registrations'}
controller.rb
def team_search
@users = Team.search(params[:search])
end
视图
<%= form_tag teams_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
答案 0 :(得分:0)
您可以使用单独的控制器和路径:
class TeamsController
def search
# ...
end
end
# routes.rb
resources :team do
collection do
get :search
end
end