我尝试在同一页面上实现两个search form_tag
,每个搜索表单都放在dynamic bootstrap tabs
内。第一个正在工作的是一个带有一个字段的搜索表单。第二个不起作用的字段有两个字段,一个是与第一个相同的搜索方法,另一个是我尝试从other_location
字段和params[:other_location]
获取地址。
使用当前设置,第二种形式的other_location
字段不会出现!
两个表单都在partials中,我将它们放在两个dynamic bootstrap tabs
里面,如下所示:
<%= render 'pages/search' %>
<%= render 'pages/search_other' %>
<%= form_tag search_items_path, :method => "get" do %>
<%= text_field_tag :search, params[:search], autofocus: true,
class: "search-query search_size",
placeholder: "Enter product to search" %>
<%= submit_tag "Search", name: nil, :style => "display: none;" %>
<%end%>
<%= form_for :search_other_path, :method => "get" do |form| %>
<%= form.text_field :search, autofocus: true,
class: "search-query search_size",
placeholder: "Enter keyword to search" %>
<% form.fields_for :other_location_path, :method => "get" do |f| %>
<%= f.text_field :other_location, class: "search-query search_size",
placeholder: "Enter address to search" %>
<%= form.submit "Search", name: nil, :style => "display: none;" %>
<%end%>
<%end%>
模型
def self.search(search)
return where("0=1") if search !~ /\w{4}/
where("lower(title) LIKE lower(:term)", term: "%#{search}%")
end
的routes.rb
get 'search' => 'pages#search', as: 'search_posts'
get 'search' => 'pages#search_other', as: 'search_other'
get 'search' => 'pages#other_location', as: 'other_location'
控制器:
def search_other
if params[:search]
@posts = Post.near(other_location,10).search(params[:search]).page(params[:page])
else
@posts = []
end
end
def other_location
other_location = params[:other_location]
if params[:other_location]
Geocoder.search(params[:other_location])
end
end
def search
if params[:search]
@posts = Post.near(action,10).search(params[:search]).page(params[:page])
else
@posts = []
end
end
答案 0 :(得分:1)
在路线档案中:
get 'search/other' => 'pages#search_other', as: 'search_other'
get 'search' => 'pages#search_other', as: 'search_other_items'
两个GET请求都将转到您的pages_controller.rb#search_other
方法。因此,即使您有两个form_tag
将数据发送到不同的路径(search_other_path
和search_other_items_path
),它也将采用相同的控制方法 - 这是多余的。
在您的实际HTML上,您有两个表单标记:
<%= form_tag search_items_path, :method => "get" do %>
和
<%= form_tag search_other_items_path, :method => "get" do %>
你的路线中没有提到search_items_path
,所以我不知道它指向哪里。可能它是一个适当的控制器,因为你提到第一个表单是唯一有效的。
现在,您提到的控制器只有search
方法。所以开始你看错了控制器。您应该查看表单操作引用的控制器方法。
在这种情况下,第二种表单是将其请求发送给search_other_items_path
,根据您的路由,它指向pages_controller.rb
- &gt; #search_other
方法。
您应该编辑问题以包含实际相关的代码。也许那时我真的可以帮忙。