【问题标题】:Rails | Too many redirects导轨 |重定向过多
【发布时间】: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


【解决方案1】:

'Show' 是一个 'get' 方法,并且有自己的要呈现的视图。在 'show' 方法中使用 'redirect' 会给你无限的重定向错误。所以,

use 'render' instead of 'redirect'

例如

render 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] 等参数仍然可以在视图中访问,而无需像在方法中那样将它们作为参数传递。

【讨论】:

  • 感谢您的评论。重定向的原因主要是因为我想要 2 个 url。 search/country/.. 和 search/country/city.. 但如果我使用渲染方法,我必须有另一个视图(可能与 show.html.erb 相同)。
  • 我已经尝试过了,我在那个显示页面中有表单(ransack),我也把它放到了 search_city.html.erb。它现在给出错误;没有向 search_form_for 提供 Ransack::Search 对象!
  • 你在控制器的 show 方法中初始化 Ransack 对象了吗?
  • 编辑问题添加整个动作
  • 首先分配参数,然后您才能在您的方法中使用重定向或渲染。否则在分配对象之前,页面将被重定向或呈现,因此下面的参数将不可用。我希望你明白我的意思。代码 if BoatGroup.friendly.find(params[:boat_group_id]).name == 'All' 把它们放在第一位,然后有重定向或渲染的代码。
猜你喜欢
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
相关资源
最近更新 更多