配置用户url,其中包含用户名,但不关联模型
使用 Rails 2.3.8 和Friendly_id。
人员模型:
class Person < ActiveRecord::Base
# Nice URL's in Rails.
def to_param
"#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
end
end
人员控制器:
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
用户模型(已删除):
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
People.rb
和 User.rb
中的 to_param
定义不起作用。
Users
控制器下的所有页面纯粹用于用户的帐户设置并且用于私人视图,因此我不需要其中任何漂亮的 URL。但我创建了一个 People
控制器来展示用户的个人资料以供公众查看。
people
URL 当前为 http://abc.com/people/1
,我想将其转换为 http://abc.com/people/ login-name-here
,但又不会破坏 people
控制器中的 @person = User.find(params[:id])
因为我需要在其中执行狮身人面像搜索。
我可以做什么来实现它?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您已经解决了这个问题,希望这对其他人有帮助。
如果使用Friendly_id,它不应该破坏
User.find(params[:id])
。事实上,person/login-name
和person/ 123 条路线均适用。
因为您使用的是 Rails 2.3.8,所以您需要使用Friendly_id 版本 3.2.1.1 是支持 ActiveRecord 2.3.x 的最新版本 - (在此处找到 https://rubygems.org/gems/Friendly_id/versions/3.2.1.1)。版本 4.x 需要 activerecord 3+,因此不兼容。
我建议添加
cached_slug
列(作为string)然后,接下来...
只需将以下内容添加到
app/models/user.rb
中,您应该能够摆脱模型上的 to_param 方法。
请记住更新 attr_accessible 以避免出现问题。
此外,您还可以创建一个实例方法,该方法返回一个与Friendly_id 一起使用的字符串,例如:
然后只需添加:
FriendlyId 为您小写slug。
注意:我记得在 attr_accessible 项目之前包含 has_Friendly_id 存在问题。我几乎可以肯定它需要放在该行之后,但我现在似乎不记得了。
Hoping this will help someone else coming here, if you have already solved this.
It shouldn't break the
User.find(params[:id])
if using friendly_id .. In fact, bothperson/login-name
andperson/123
routes with both work.Because you're using rails 2.3.8, You will need to use friendly_id version 3.2.1.1 is the latest version that supports activerecord 2.3.x - (found here https://rubygems.org/gems/friendly_id/versions/3.2.1.1). Version 4.x needs activerecord 3+ so won't be compatible.
on your user table I recommend adding a
cached_slug
columns (as a string)Then, following that ...
Just add the following to
app/models/user.rb
and you should be able to get rid of the to_param methods on your models.
Remember to update attr_accessible to avoid issues.
Also, you can create an instance method that returns a string for use with friendly_id, eg:
Then just add:
FriendlyId downcases the slug for you.
NOTE: I recall there being a problem with including the has_friendly_id before the attr_accessible items. I'm almost certain it needs to be placed after that line, but I can't seem to recall at the moment.