0

I have URLs like this

arizona/AZ12
colorado/CO470

I added the AZ and CO because friendly id wanted unique ids. Arizona and Colorado could have a unit 12.

I'd like to have URLs like

arizona/unit12
colorado/unit470

Seems like you could write something that removes the first two characters and replaces them. Would that be in the routes or controller?

My routes

resources :states, :except => [:index ], :path => '/' do
    resources :units, :except => [:index ], :path => '/'
  end

My controller

  def show
    @units = Unit.all
     @states = State.with_units.group('states.id')
        @state = State.all
        @unit = Unit.friendly.find(params[:id])
  end
Mike Campbell
  • 7,921
  • 2
  • 38
  • 51

1 Answers1

0

Implement to_param method on your model. Rails will call to_param to convert the object to a slug for the URL. If your model does not define this method then it will use the implementation in ActiveRecord::Base which just returns the id.

class SomeModel
  def to_param
    "unit#{id}"
  end
end

You can refer https://gist.github.com/agnellvj/1209733 for example

usha
  • 28,973
  • 5
  • 72
  • 93