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.
When I check if there is city_id
and redirect to show action again, it start infinite redirection. 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. So,
use 'render' instead of 'redirect'
For eg
render search_city will render a html page named search_city.html.erb. render search_country will render a html page named search_country.html.erb. And you can add the view you want inside the above html pages. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.