简体   繁体   English

滑轨| 重定向过多

[英]Rails | Too many redirects

I have routes such as; 我有诸如

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

My problem is, when there is city as well, I should redirect to search_city_path but both routes are handled at search#show action. 我的问题是,当还有城市时,我应该重定向到search_city_path但是两条路线都在search#show动作下处理。

When I check if there is city_id and redirect to show action again, it start infinite redirection. 当我检查是否有city_id并重定向以再次显示操作时,它将开始无限重定向。 How can I prevent this? 我该如何预防?

Here is my show action; 这是我的表演动作;

  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  
....

EDIT 编辑

Show action; 显示动作;

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' is a 'get' method and has its own views to be rendered. “显示”是一种“获取”方法,具有自己的视图。 Using 'redirect' inside 'show' method will give you infinite redirection error. 在“ show”方法中使用“ redirect”将给您无限重定向错误。 So, 所以,

use 'render' instead of 'redirect'

For eg 例如

render search_city will render a html page named search_city.html.erb. 渲染search_city将渲染一个名为search_city.html.erb的html页面。 render search_country will render a html page named search_country.html.erb. render search_country将呈现一个名为search_country.html.erb的html页面。 And you can add the view you want inside the above html pages. 您可以在上述html页面中添加所需的视图。 So the code will be 所以代码将是

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 

And the params like @country, @city, @rental_type, @group, params[:q] can still be accessed in the view without passing them as params as you have done in your method. 像@ country,@ city,@ rental_type,@ group,params [:q]这样的参数仍然可以在视图中访问,而无需像在方法中那样将它们作为参数传递。

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

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