我已在一个页面中编码,其中拉出数据库中的数据,然后当我点击显示的内容时,它会在应用程序的搜索功能中输入相应的值并显示结果,代码可以是如下所示:
课程观点:
<!-- Index of all Courses -->
<% provide(:title, "Course") %>
<!--containers for design/layout -->
<div class = "signinstyle">
<div class = "row">
<!--Page information -->
<%= form_tag(degree_new_path, :method => "get", id: "search-data") do %>
<table border="1" class="table">
<thead>
<tr>
<th>Courses</th>
</tr>
</thead>
<tbody>
<% @ads.each do |degree| %>
<tr>
<td> <%= link_to degree.cname, keyword_search_path(search: degree.cname) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Select" %>
<% end %>
<!--closing the design/layout containers -->
</div>
</div>
degree controller (the above view is within this):
class Degree < ActiveRecord::Base
def Degree.search(search)
where("cname LIKE ? OR ucas LIKE ?", "%#{search}%", "%#{search}%")
end
end
搜索控制器(因为我在视图中使用了我的关键字搜索):
def keyword_search
@search = Degree.all.select(:uname, :cname, :ucas, :duration, :qualification, :entry).distinct.order(id: :ASC)
if params[:search]
@search_degree = Degree.search(params[:search]).order('cname ASC')
end
end
def course
@select = Degree.all.select(:uname, :cname, :ucas, :duration, :qualification, :entry).distinct.order(id: :ASC)
if params[:search]
@select_degree = Degree.search(params[:search])
end
end
我试图复制上面的代码,这样我就可以点击类似的链接,这些链接会将数据输入到我所拥有的搜索功能搜索功能中,但一直无法这样做,如果有人可以帮助我,那将不胜感激。以下是我目前正在努力开展工作的代码:
搜索控制器:
def adsearch
@adsearch = Degree.ransack(params[:q])
@data = @adsearch.result
@adsearch.build_condition if @adsearch.conditions.empty?
@adsearch.build_sort if @adsearch.sorts.empty?
end
视图文件:
<!-- Index of all Courses -->
<% provide(:title, "Course") %>
<!--containers for design/layout -->
<div class = "signinstyle">
<div class = "row">
<!--Page information -->
<%= form_tag(degree_new_path, :method => "get", id: "search-data") do %>
<table border="1" class="table">
<thead>
<tr>
<th>Courses</th>
</tr>
</thead>
<tbody>
<% @ads.each do |degree| %>
<tr>
<td> <%= link_to degree.subject_group, adsearch_path(name: ucas & value: degree.ucas_letter) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Select" %>
<% end %>
<!--closing the design/layout containers -->
</div>
</div>
使用代码的最后两个代码显示我要求它在初始视图上显示的内容,但是没有输入我希望它进入搜索范围搜索的值,因此没有&#39 ; t点击时创建搜索,就像第一个例子那样。