【发布时间】:2017-12-15 02:21:24
【问题描述】:
我有这样的路线;
scope to: 'search#show' do
get '/search/:country_id/:rental_type_id/:group_id', as: 'search_country'
get '/search/:country_id/:city_id/:rental_type_id/:group_id', as: 'search_city'
end
我的问题是,当还有城市时,我应该重定向到 search_city_path 但两条路线都在 search#show 操作处处理。
当我检查是否有 city_id 并重定向以再次显示操作时,它会开始无限重定向。我怎样才能防止这种情况发生?
这是我的表演动作;
def show
@rental_type = RentalType.friendly.find(params[:rental_type_id])
@group = Group.friendly.find(params[:group_id])
if !params[:boat_city_id].blank?
@country = Country.friendly.find(params[:country_id])
@city = City.friendly.find(params[:city_id])
redirect_to search_city_path(country_id: @country, city_id: @city, rental_type_id: @rental_type, group_id: @group, q: params[:q])
else
@country = Country.friendly.find(params[:country_id])
end
....
编辑
显示动作;
def show
@rental_type = RentalType.friendly.find(params[:rental_type_id])
@boat_group = BoatGroup.friendly.find(params[:boat_group_id])
if !params[:new_boat_country_id].nil? && params[:new_boat_country_id] != params[:boat_country_id]
@country = BoatCountry.friendly.find(params[:new_boat_country_id])
redirect_to search_country_path(boat_country_id: @country, rental_type_id: @rental_type, boat_group_id: @boat_group, q: params[:q])
elsif !params[:boat_city_id].blank?
@country = BoatCountry.friendly.find(params[:boat_country_id])
@city = BoatCity.friendly.find(params[:boat_city_id])
render 'search_city'
else
@country = BoatCountry.friendly.find(params[:boat_country_id])
end
if BoatGroup.friendly.find(params[:boat_group_id]).name == 'All'
if params[:boat_city_id].blank?
b = @country.boats.where("(stat = ? AND boat_category = ?)", "Approved", "list" )
else
@city = BoatCity.friendly.find(params[:boat_city_id])
b = @city.boats.where("(stat = ? AND boat_category = ?)", "Approved", "list" )
end
else
b = @boat_group.boats.where("(stat = ? AND boat_category = ?)", "Approved", "list" )
end
@q = b.ransack(params[:q])
@boats = @q.result(distinct: true).paginate(:page => params[:page], :per_page => 15)
end
【问题讨论】:
-
为什么还要
redirect_to search_city_path?基于params[:city_id]的存在,可以改为查询显示搜索结果
标签: ruby-on-rails redirect controller routes