繁体   English   中英

滑轨| 重定向过多

[英]Rails | Too many redirects

我有诸如

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

“显示”是一种“获取”方法,具有自己的视图。 在“ show”方法中使用“ redirect”将给您无限重定向错误。 所以,

use 'render' instead of 'redirect'

例如

渲染search_city将渲染一个名为search_city.html.erb的html页面。 render search_country将呈现一个名为search_country.html.erb的html页面。 您可以在上述html页面中添加所需的视图。 所以代码将是

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])
      render 'search_country'
    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 

像@ country,@ city,@ rental_type,@ group,params [:q]这样的参数仍然可以在视图中访问,而无需像在方法中那样将它们作为参数传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM