如何使用重定向到搜索页面.问题:err_to_many_redirects



我正试图在我的网站上添加一个搜索栏,并在没有输入参数的情况下重定向到带有警告的搜索页面。

现在,当我点击搜索图标时,它会把我带到搜索页面,这样我就知道路径存在了。当我将搜索方法添加到ApplicationController时,我会得到err_to_many_redirects,这意味着某个地方有一个无限。

我还尝试重定向到我的根页面,这很有效,但警报没有发生。

countries_controller.rb

class CountriesController < ApplicationController
def search
if params[:search].blank?
redirect_to(search_page_path, alert: "Empty field!") and return
#redirect_to(root_path, alert: "Empty field!") and return
else
end
end

end

routes.rb

Rails.application.routes.draw do
get 'countries/index'
root 'countries#index'
get 'countries/search', to: 'countries#search', :as => 'search_page'
end

application.html.erb

<%= form_tag(search_page_path, :method => "get", class: 'navbar-form navbar-right') do %>
<div class="input-group">
<%= search_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
<div class="input-group-btn">
<%= button_tag "", :class => "btn btn-info glyphicon glyphicon-search", :name => nil %>
</div>
</div>
<% end %>

耙式路线

Prefix Verb   URI Pattern                                                                              Controller#Action
countries_index GET    /countries/index(.:format)                                                               countries#index
root GET    /                                                                                        countries#index
search_page GET    /countries/search(.:format)                                                              countries#search

我想我已经包括了所有相关的内容,但我是新手,所以如果有什么遗漏,请告诉我。

您当前正在做的是递归地从搜索页面重定向到它自己,这将创建一个无限循环。

由于你已经在搜索页面上了,你只需要显示警报,而不需要进行任何进一步的重定向。为了显示警报,您可以使用flash消息。

def search
if params[:search].blank?
flash.now[:alert] = "Empty field!"
else
...
end
end

你可以在这里阅读更多关于短信

相关内容

最新更新