导轨|重定向太多

时间:2017-07-11 06:56:59

标签: ruby-on-rails redirect controller routes

我有路线,如;

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/your_ripple_color">

    <!-- button background -->
    <item android:drawable="@drawable/abc"/>
</ripple>

我的问题是,当有城市时,我应该重定向到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  
....

1 个答案:

答案 0 :(得分:0)

&#39;显示&#39;是一个&#39; get&#39;方法并有自己的要渲染的视图。使用&#39;重定向&#39;里面&#39;显示&#39;方法会给你无限的重定向错误。所以,

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