gpt4 book ai didi

ruby-on-rails - ruby rails : Advanced search

转载 作者:数据小太阳 更新时间:2023-10-29 06:58:32 25 4
gpt4 key购买 nike

我无法理解制作高级搜索表单的最佳方式。我在互联网上进行了很好的搜索,寻找了一些方法,但我无法让它们发挥作用,因为大多数建议都已过时。我已经问了一个问题,但我认为我太具体了,我无法解决我的问题。我想用一个搜索按钮搜索不同的文本框和下拉框。

编辑2:

项目 Controller :

def index
@projects = Project.all

respond_to do |format|
format.html # index.html.erb
format.json { render :json => @projects }
end
end

def search
@project_search = Project.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 2, :page => params[:page])


end




# GET /projects/1
# GET /projects/1.json
def show
@project = Project.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @project }
end
end

# GET /projects/new
# GET /projects/new.json
def new
@project = Project.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end

# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
end

# POST /projects
# POST /projects.json
def create

@project = Project.new(params[:project])


@project.client = params[:new_client] unless params[:new_client].blank?
@project.exception_pm = params[:new_exception_pm] unless params[:new_exception_pm].blank?
@project.project_owner = params[:new_project_owner] unless params[:new_project_owner].blank?
@project.role = params[:new_role] unless params[:new_role].blank?
@project.industry = params[:new_industry] unless params[:new_industry].blank?
@project.business_div = params[:new_business_div] unless params[:new_business_div].blank?

respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render json: @project, status: :created, location: @project }
else
format.html { render action: "new" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end

# PUT /projects/1
# PUT /projects/1.json
def update
@project = Project.find(params[:id])

respond_to do |format|
if @project.update_attributes(params[:project])
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end

# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project = Project.find(params[:id])
@project.destroy

respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :no_content }
end
end




private

helper_method :sort_column, :sort_direction
def sort_column
Project.column_names.include?(params[:sort]) ? params[:sort] : "project_name"
end

def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end

搜索 View :

<h1>Search</h1>

<%= form_tag search_path, method: :get do %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<%= text_field_tag :project_name, params[:project_name] %>
<%= text_field_tag :client, params[:client] %>
<%= submit_tag "Search", name: nil %>
<% end %>



<table class = "pretty">
<table border="1">
<tr>
<th><%= sortable "project_name", "Project name" %> </th>
<th><%= sortable "client", "Client" %></th>
<th>Exception pm</th>
<th>Project owner</th>
<th>Tech</th>
<th>Role</th>
<th>Industry</th>
<th>Financials</th>
<th>Business div</th>
<th>Status</th>
<th>Start date</th>
<th>End date</th>
<% if false %>
<th>Entry date</th>
<th>Edited date</th>
<th>Summary</th>
<th>Lessons learned</tStackh>
<th>Customer benifits</th>
<th>Keywords</th>
<!th></th>
<!th></th>
<!th></th>
<% end %>
</tr>

<% @project_search.each do |t| %>
<tr>
<td><%= t.project_name %></td>
<td><%= t.client %></td>
<td><%= t.exception_pm %></td>
<td><%= t.project_owner %></td>
<td><%= t.tech %></td>
<td><%= t.role %></td>
<td><%= t.industry %></td>
<td><%= t.financials %></td>
<td><%= t.business_div %></td>
<td><%= t.status %></td>
<td><%= t.start_date %></td>
<td><%= t.end_date %></td>
<% if false %>
<td><%= t.entry_date %></td>
<td><%= t.edited_date %></td>
<td><%= t.summary %></td>
<td><%= t.lessons_learned %></td>
<td><%= t.customer_benifits %></td>
<td><%= t.keywords %></td>
<% end %>
<!td><%#= link_to 'Show', project %></td>
<!td><%#= link_to 'Edit', edit_project_path(project) %></td>
<!td><%#= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>

<br />
<%= will_paginate (@project_search) %>


<%= button_to "Search Again?", search_path, :method => "get" %>

<%# end %>
<%= button_to "Home", projects_path, :method => "get" %>

项目.rb

class Project < ActiveRecord::Base
attr_accessible :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech

validates_presence_of :business_div, :client, :customer_benifits, :end_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech



def self.search search_term
return scoped unless search_term.present?
where find(:all, :conditions => ['project_name OR client LIKE ?', "%#{search_term}%"])
end

end

路线:

FinalApp::Application.routes.draw do
resources :projects
match "search" => "projects#search", :as => :search
root :to => 'projects#index'
end

如您所见,我距离完成应用程序还有一段距离。我正在尝试制作一个搜索表单,该表单将能够搜索以下字段:项目名称、客户、ID、行业、角色、技术、项目所有者、状态、开始日期、结束日期和关键字。搜索表单将包含文本框或下拉菜单,具体取决于用户搜索的字段。我想链接每个字段并一次性搜索它们。之前我只是以project_name和client为例,方便大家看懂我的代码。希望您现在可以看到我正在尝试做的事情。

最佳答案

您可以创建一个名为 search 的新 Controller 。

您的搜索表单:

<%= form_tag search_index_path, method: :get do %>
<%= text_field_tag :project, params[:project] %>
<%= text_field_tag :client, params[:client] %>
<%= submit_tag "Search", name: nil %>
<% end %>

包含在你的 routes.rb 中:

get "search/index"

你的搜索 Controller :

def index
#store all the projects that match the name searched
@projects = Project.where("name LIKE ? ", "%#{params[:project]}%")
#store all the clients that match the name searched
@clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
end

现在您可以在索引 View 中使用 @projects@clients

请小心,因为如果没有匹配项,这些变量可能会变为 nil。

编辑 - 我假设你有两个模型 ProjectClient - 如果你不能创建一个新的 Controller ,你可以创建搜索操作在您当前的 Controller 中。

def search
#store all the projects that match the name searched
@projects = Project.where("name LIKE ? ", "%#{params[:project]}%")
#store all the clients that match the name searched
@clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
end

然后您可以在搜索 View 中使用 @projects@clients

如果您试图在其他地方显示结果(例如 index View ),您只需将上面的内容移动到正确的操作即可。

 def index
....
#store all the projects that match the name searched
@projects = Project.where("name LIKE ? ", "%#{params[:project]}%")
#store all the clients that match the name searched
@clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
end

编辑 2 - 好的,您正在尝试通过同一模型中的字段组合进行搜索:

您并更改您的搜索方法以添加这两个字段:

def self.search(search_project, search_client) 
return scoped unless search_project.present? || search_client.present?
where(['project_name LIKE ? AND client LIKE ?', "%#{search_project}%", "%#{search_client}%"])
end

但请注意,如果您的 search_project 或 search_client 不存在,|| 将返回范围,您可以根据需要更改为 AND (&&)。

此外,AND 只有在两者匹配时才会返回,我的意思是搜索的组合...如果需要,您也可以将其更改为 OR

具有搜索表单:

您的搜索表单:

<%= form_tag search_index_path, method: :get do %>
<%= text_field_tag :project, params[:project] %>
<%= text_field_tag :client, params[:client] %>
<%= submit_tag "Search", name: nil %>
<% end %>

然后您的 Controller 必须将组合发送给模型:

@project_search = Project.search(params[:project], params[:client]).all

我认为它会解决问题...

关于ruby-on-rails - ruby rails : Advanced search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11609005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com