Using Rails 2.3.8, and friendly_id.
Person model:
class Person < ActiveRecord::Base
# Nice URL's in Rails.
def to_param
"#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
end
end
People controller:
class PeopleController < ApplicationController
def show
@person = User.find(params[:id])
if current_user == @person
@shops = @person.shops.paginate(:page => params[:page], :order => order)
else
@shops = @person.shops.by_status('published').paginate(:page => params[:page], :order => order)
end
end
end
User model (stripped):
class User < ActiveRecord::Base
extend ActiveSupport::Memoizable
acts_as_authentic do |c|
c.validate_email_field = false
c.validate_login_field = false
end
attr_accessible :login, :email, :password, :password_confirmation, :name, :location, :website,
:allow_follower, :allow_message, :allow_updates, :allow_newsletter, :avatar, :role, :remember_me
# Returns name or login if name is blank
def full_name
if self.name.blank?
login.strip
else
(self.name || '').strip
end
end
memoize :full_name
# Nice URL's in Rails.
def to_param
"#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
end
end
The to_param
definition in People.rb
and User.rb
don't work.
All pages under Users
controller are purely for user's account settings and is for private view, so I don't need any nice URL in it. But I have created a People
controller to showcase as the user's profile for public view.
The people
URL currently is http://abc.com/people/1
, I want to turn it to http://abc.com/people/login-name-here
, yet without breaking the @person = User.find(params[:id])
in people
's controller because I need to perform a sphinx search in it.
What can I do to achieve it? Thank you.