0

I prettified my urls using friendy_id. My old url used look like below:

localhost:3000/search?category_id=208

Now it looks like this:

localhost:3000/search?category_id=metal-processing-and-machine-tool

My controller is this:

def search_equipments
    begin
      if (params.keys & ['category_id', 'sub_category', 'manufacturer', 'country', 'state', 'keyword']).present?
        if params[:category_id].present?
          @category = Category.active.friendly.find_by_friendly_id params[:category_id]
        else
          @category = Category.active.friendly.find_by_friendly_id params[:sub_category] if params[:sub_category].present?
        end
        @root_categories = Category.active.roots
        @sub_categories = @category.children.active if params[:category_id].present?
        @sub_categories ||= {}
        @countries = Country.active.all
        @manufacturers = Manufacturer.active.all
        @states = State.active.where("country_id = ?", params[:country]) if params[:country].present?
        @states ||= {} 
        unless params[:category_id].present? && params[:sub_category].present?
          params[:category_id] = @category.id if params[:category_id].present?
          params[:sub_category] = @category.id if params[:sub_category].present?
        end   
        @equipments = Equipment.active.filter(params.slice(:manufacturer, :country, :state, :category_id, :sub_category, :keyword)).order("#{sort_column} #{sort_direction}, created_at desc").page(params[:page]).per(per_page_items)               
      else
        redirect_to root_path
      end
    rescue Exception => e
      render :file=>"#{Rails.root}/public/404.html",:status=>404
    end

  end

Right now I am redirecting old urls to 404 page. But instead I would like the old urls to redirect to new urls. How can I achieve this? I gone through some solutions. But my big controller method is making it complex enough to follow those solutions.

user3576036
  • 1,335
  • 2
  • 23
  • 51

2 Answers2

1

add this to your method search_equipments beginning

   if params[:category_id].present? and params[:category_id].to_s !~ /\D/
      category = Category.find_by_id(params[:category_id])
      return redirect_to search_equipments_path(category_id: category.name.parameterize) if category.present?
   end
  • Added next line after `begin`. But its still going to 404 when given old url. – user3576036 Apr 26 '17 at 07:04
  • no, before the`begin`, because i might make a typo error, so it get exception. and it's bad habit to catch unknown exception, you should really know what could happen. sometimes just let it happen, so you can know what's the problem. – seaify - Freelancer Apr 26 '17 at 07:21
  • Okay I added it before `begin`. You mentioned `search_category_path` in your answer. I think you meant to say `search_equipments_path` so I changed it. Anyway so now when I give old url it is redirecting to home page. – user3576036 Apr 26 '17 at 07:30
  • yep, if it's working, make it as answer. that path you need to change to what you want, because i don't know exactly what it is. – seaify - Freelancer Apr 26 '17 at 07:35
  • No its not working yet. If I am giving the path as `search_equipments_path` then it should land on that path. Instead it is going to home page. – user3576036 Apr 26 '17 at 07:39
  • check if there exists some callback, which hooks it, do the things that redirect to home. take care of the logs. it's another problem now. – seaify - Freelancer Apr 26 '17 at 07:41
  • So basically after implementing your solution old urls are redirecting to home page. I am trying to redirect them to new url not home page. No there are no `callbacks` on that controller. – user3576036 Apr 26 '17 at 07:43
  • This is what's going on in the console `Processing by WelcomeController#search_equipments as HTML Parameters: {"name"=>"Construction and Heavy Equipment"} Redirected to http://localhost:3000/` When give old url with id number. – user3576036 Apr 26 '17 at 07:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142696/discussion-between-seaify-freelancer-and-user3576036). – seaify - Freelancer Apr 26 '17 at 08:02
  • One more thing. Is it possible to do this from `routes.rb` file instead of filling controller method with so much? – user3576036 Apr 26 '17 at 10:41
  • no, i think you can't and shouldn't, logic should not exists at `routes.rb`, it should only care which path to which controller and action. – seaify - Freelancer Apr 26 '17 at 10:42
  • Is there any way that this can be achieved using `friendly_id` itself? Your solution is converting category name from the db and `parameterize` ing them and redirecting and not using `slug` from `freindly_id`. The reason I am asking is, this is affecting other things on the code. – user3576036 Apr 27 '17 at 05:00
1

You can try this:

if params[:category_id].present? and params[:category_id].to_s !~ /\D/
       category = Category.find_by_id(params[:category_id])
       return redirect_to search_equipments_path(request.query_parameters.merge(category_id: category.name.parameterize)) if category.present?

If you have sub-category, you can use same condition just changing params category to sub-category.

Manish Nagdewani
  • 597
  • 1
  • 3
  • 14